ios - Why do clone variables still affect the original object? SWIFT -


firstly, apologies if question title didn't make sense, finding tricky word correctly! i'm pretty confident in objective-c new project i've had take on swift (something i've been pretty apprehensive about).

anyway, i've noticed on simple tableviewcontroller tutorial driving me crazy.

first new class set each row both 'text' , 'checked' variable. array made of objects of class populate table rows.

but in tutorial maker's code, changes 'checked' property of particular row using:

 let item = items[indexpath.row]   item.checked = !item.checked 

where items[indexpath.row] picks out required object in array items. confused, surely first line creates new clone object in no other way linked object in array?

however, when changing of item's variables (e.g. how swaps state of item.checked change applied original array object items[indexpath.row] makes no logical sense?

surely have implement kind of logic make happen, example:

items[indexpath.row] = item 

to apply changes item original object? can please explain backwards connection apparently there?

thank in advance, i'm missing simple!

look difference between value types , reference types. have reference type. classes reference types.

//new class init var myclassinstance : myclass = myclass()  // creates new reference original object // changes happen original var myclone = myclassinstance 

structs example value types. these copy values , not tied original

work around :

you can create custom initialiser takes necessary values original class.

class mycustomimageview : uiimageview {      override init(frame: cgrect) {          super.init(frame: frame)     }      init(uiimageview_i : uiimageview) {          super.init(frame:uiimageview_i.frame)          self.image = uiimageview_i.image     }      required init(coder adecoder: nscoder) {         fatalerror("init(coder:) has not been implemented")     }  } 

when call custom initialiser instead of standard one, create separate instances each time.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -