1

Perhaps this is a silly question and I'm completely misunderstanding how passing variable in Swift works, but I was under the impression that Swift was pass by copy by default, and you could use the inout keyword to denote passing by reference.

How come then, if I have a function that has a UIView parameter, I can change, say, the backgroundColor of the UIView within that function, and it's reflected outside the function. If the function creates a local copy when passed, how is this affecting the passed variable?

Why does that not require inout?

Doug Smith
  • 28,841
  • 56
  • 200
  • 373

1 Answers1

1

Because the UIView you pass around is a class instance: it is a pointer to begin with. So, in essence, you are passing that pointer by value, since you are not passing a reference to that pointer (like &pointer in C).

See Is Swift Pass By Value or Pass By Reference for an elaborate answer.

Community
  • 1
  • 1
SwiftArchitect
  • 45,394
  • 26
  • 137
  • 173