-1

I have a very simple question on something that I may have misunderstood.

I have two UIViews "A" and "B". If I write :

let A = UIView() // Or something else
let B = A

and then I change properties of B (for exemple the frame), will the properties of A change too ?

I though not, but I was animating a view, so I had the initial view and the final view. I created a transition view like this :

let transitionView = finalView

and then I changed the properties of transitionView, the position of a label for exemple.

When I added the final view at the end of the animation, the label was at the new position.

Why ? Thanks

Morpheus
  • 1,189
  • 2
  • 11
  • 33
  • 1
    This is an incredibly elementary feature of Swift. Please try to consult the documentation before using bandwidth on a question like this. – matt Dec 06 '14 at 19:55
  • This question appears to be off-topic because it is about not even trying to consult the documentation. – matt Dec 06 '14 at 19:55

2 Answers2

2

Because B and A are not two views. They are references to the same UIView object.

That, in turn, is because class instances are passed as reference types in Swift.

See now my little essay on this topic here: https://stackoverflow.com/a/27366050/341994

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • What am I supposed to search in documentation ? Instead of criticizing, could you tell me a few words that I could search ? – Morpheus Dec 06 '14 at 20:09
  • This is an instance of a _class_. Read the Swift documentation page about _classes_. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_145 Focus in particular, for this matter, on the place where it says in big letters: **Classes Are Reference Types**. – matt Dec 06 '14 at 20:16
  • @Coconuts Welcome to Stack Overflow! You'll enjoy being here more if you take things less personally. There is no criticism in this answer. It's just an answer to the question you asked! – matt Dec 06 '14 at 21:45
  • I was referring to your commentaries to my question. Thanks for your help – Morpheus Dec 07 '14 at 12:38
  • 1
    My comments were not a criticism either. They were sound advice on how to program, how to solve problems yourself, and how to ask a good Stack Overflow question. – matt Dec 07 '14 at 15:10
2

In swift types are split in 2 main categories:

  • reference types
  • value types

Classes are reference types; structs (which include arrays and dictionaries), basic data types (int, float, string, etc.), and enums are all value types.

A value type is always passed by value, which means when assigning to a variable or passing to a function/method, a copy of the original data is created. There's an exception to this rule: a function/method can use the inout modifier on a value type parameter to have it passed by reference.

Note that the compiler and the runtime usually do optimizations, so a copy is not always created unless strictly needed - what's important is that we, as developer, know that we are working on a copy and not on the original data.

A reference type is always passed by reference, which means when assigning it to a variable or passing it to a function/method, a reference to the data and not the data itself is assigned/passed.

UIView is a class, so when you create an instance, assign it to a variable, then assign that variable to another variable, the reference to the instance and not the instance itself is assigned. Both variables point to the same UIView instance. Any change made to the instance is visible to all variables referencing that instance.

Suggested reading: Classes and Structures

Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Ok, thanks, I realize I didn't understand all of this... So how can I duplicate a view then ? How can I have a second view that I can modify without modifying the first ? – Morpheus Dec 06 '14 at 20:32
  • Just create another instance - instead of `let b = a`, `let b = UIView()`. If you actually want a copy of `a`, just google for `create copy UIView` - maybe [this answer](http://stackoverflow.com/a/13756101/148357) does the job, but never tried that. – Antonio Dec 06 '14 at 20:40
  • 1
    @Coconuts If you have a new question that arises from your understanding of this question's answer, please ask a new question! Also, if you have been helped by an answer, please consider accepting it (checkmark). – matt Dec 06 '14 at 21:44
  • @Antonio That's not accurate. We've just had a discussion about that here http://stackoverflow.com/questions/27364117/is-swift-pass-by-value-or-pass-by-reference and it is wrong to say that reference types are passed by reference. They are passed by value, but the value is the value of a reference (think pointer). It is also wrong to say that a value type is always passed by value, since you can pass it by reference if you want to (with `inout`). It is tempting to confuse "reference type" with "pass by reference", and I have confused them myself - but don't. – matt Dec 09 '14 at 20:05
  • @matt Well there is a lot to say on that subject. From a dev point of view, what I need to know is that when I pass a class instance to a function, I am passing a reference to the instance and not an instance copy - and when I pass a value type, I am passing a copy of it (regardless of optimizations made by the compiler or runtime to prevent actual copies). So I consider more intuitive if I say "A reference type is passed by reference" rather than a more exhaustive explanation of how that is done. I think that giving too many details can be more confusing when explaining the mere concept. – Antonio Dec 10 '14 at 08:39