简体   繁体   English

Objective-C如何在rootViewController和modalViewController之间同步两个对象

[英]objective-c how to synchronize two objects between rootViewController and modalViewController

I'm new to OOP and I'm confused about this following line on chapter 6 of head first iphone development: 我是OOP的新手,我对第一部iphone开发的第6章中的以下内容感到困惑:

addDrinkVC.drinkArray = self.drinks addDrinkVC.drinkArray = self.drinks

the purpose is to assign the self.drinks NSMutableArray to another NSMutableArray in modal viewController addDrinkVC. 目的是将self.drinks NSMutableArray分配给模式viewController addDrinkVC中的另一个NSMutableArray。 But why when you added an object into drinkArray in modal view and returned to the rootView and reload the tableView, self.drinks also gets changed? 但是,为什么当您在模式视图中将对象添加到DrinkArray并返回到rootView并重新加载tableView时,self.drinks也会更改? Is it related to the concept of pointer? 它与指针的概念有关吗?

Thanks! 谢谢!

Unless drinkArray is declared with @property (copy) , the contents of the array are not copied. 除非用drinkArray @property (copy)声明drinkArray否则不会复制数组的内容。 This does indeed have to do with pointers. 这确实与指针有关。 Since the array is mutable, and since you are not copying it – only adding a new reference to it – any changes made to either reference will be visible on the other. 由于该数组是可变的,并且由于您没有复制它-仅对其添加新引用-对这两个引用所做的任何更改都将在另一个上可见。 In other words, there is only one actual array in use. 换句话说,只有一个实际的数组在使用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM