ios - Swift - Why does a passed custom class retain changed properties when returning to previous view? -


i've noticed odd when passing data through segue , can't figure out why way. understand i'm misunderstanding nature of classes, appreciate if in getting knowledge it.

when pass int or string second view through segue (both connected navigation controller), change value within second view, , return first one, value of int or string same set on first view, not retaining value changed in second view. makes sense , expect happen anyway.

but noticed when working custom class not case. if changed values belonged class, retain when going first view. did notice if tried replace whole class new one, not take, similar trying change whole int, why changing class' properties still work in case?

i know how pass data previous view, more can better understanding why things work this.

i've been playing around , here code better explain i'm talking about:

first view controller:

class viewcontroller: uiviewcontroller {      var number = 5     var string = "hello view 1"     var object = object(number: 5, string: "hello view 1", bool: false)      override func viewwillappear(_ animated: bool) {         super.viewwillappear(animated)          print("page 1 number: ", number)         print("page 1 string: ", string)         print("page 1 object number: ", object.number)         print("page 1 object string: ", object.string)         print("page 1 object bool: ", object.bool)     }      @ibaction func buttonpress(_ sender: any) {         self.performsegue(withidentifier: "tosecond", sender: self)     }      override func prepare(for segue: uistoryboardsegue, sender: any?) {         if segue.identifier == "tosecond" {             let vc = segue.destination as! secondviewcontroller             vc.number = number             vc.string = string             vc.object = object         }     } } 

second view controller:

class secondviewcontroller: uiviewcontroller {      var number = 0     var string = ""     var object = object()      override func viewwillappear(_ animated: bool) {         super.viewwillappear(animated)          print("page 2 number: ", number)         print("page 2 string: ", string)         print("page 2 object number: ", object.number)         print("page 2 object string: ", object.string)         print("page 2 object bool: ", object.bool)          number = 12         string = "hello view 2"          object.number = 12         object.string = "hello view 2"         object.bool = true         object = object2     } } 

console output:

page 1 number:  5 page 1 string:  hello view 1 page 1 object number:  5 page 1 object string:  hello view 1 page 1 object bool:  false page 2 number:  5 page 2 string:  hello view 1 page 2 object number:  5 page 2 object string:  hello view 1 page 2 object bool:  false page 1 number:  5 page 1 string:  hello view 1 page 1 object number:  12 page 1 object string:  hello view 2 page 1 object bool:  true 

i've searched couple days answer (if has been answered appreciate link).

since noone has answered question yet, add answer here.

but noticed when working custom class not case. if changed values belonged class, retain when going first view. did notice if tried replace whole class new one, not take, similar trying change whole int, why changing class' properties still work in case?

the reason behavior because mentioned in comments, classes reference types , hence value set properties available in places use same instance of it. imo, great advantage of classes against structs in swift. if don't want behavior can consider changing object struct type.

in case, though initializing var object = object() in secondviewcontroller, replacing instance object instance in viewcontroller vc.object = object inside prepareforsegue method. since doing this, change make object's property retained when come previous screen.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -