在 ViewController 之间传递 NSArray。是通过指针还是通过复制?
给定两个 UIViewController A 和 B。A 位于 UINavigationController 之上。 B 有 init 函数 initWithArray。 B 被创建并在 A 中 initWithArray 。然后 B 被推入 NavigationController 并位于 A 之上。
中传递的数组。
NSArray *arrayInA; //(assuming all init and everything was done)
在 A 中,这是在Making B
B *b = [[B alloc]initWithArray: arrayInA];
[self.navigationController pushViewController: B animation: YES];
在 B 中,initWithArray 的情况类似
if (self){
arrayInB = arrayFromA;
}
arrayInB 只是 B 中的私有属性
NSArray *arrayInB;
问题是,其中哪些是不同的对象? arrayInA / arrayFromA / arrayInB
编辑:我正在使用 ARC。
Given two UIViewControllers A and B. A sits on top of a UINavigationController. B has init function initWithArray. B was made and initWithArray in A. Then B was pushed into the NavigationController to sit on top of A.
In A, this is the array that was passed in
NSArray *arrayInA; //(assuming all init and everything was done)
Making B
B *b = [[B alloc]initWithArray: arrayInA];
[self.navigationController pushViewController: B animation: YES];
In B, initWithArray goes something like this
if (self){
arrayInB = arrayFromA;
}
arrayInB is just private property in B
NSArray *arrayInB;
The question is, which of these are different objects? arrayInA / arrayFromA / arrayInB
Edit: I am working with ARC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的代码中,arrayInB 指向与 arrayFromA 相同的内存地址,而 arrayFromA 指向 arrayInA。我建议更改您的代码,以便 arrayInB 保留对其接收的数组的引用。
或者
如果您希望 arrayInB 成为其自己的数组,指向与 arrayFromA 相同的对象,那么您应该这样做。
In your code arrayInB points to the same memory address as arrayFromA which points to arrayInA. I would suggest changing your code so that arrayInB keeps a reference to the array it receives.
or
If you want arrayInB to be its own array that points to the same objects as arrayFromA then you should do this.
由于您只创建了一个数组(该代码未显示,但由您的注释“假设所有 init 且所有操作均已完成”引用),因此仅存在一个实例,并且所有三个引用都指向该实例。
Since you've only created a single array (that code is not shown, but referred to by your comment "assuming all init and everything was done") only one instance exists and all three references point to that instance.
根据您提供的信息,arrayInA 应该与 arrayInB 具有相同的地址值,因此它们将指向同一对象。这是因为实例变量使用“分配”设置器语义。
您始终可以通过打印指针地址进行检查以确保:
Given the information you provide, arrayInA should have the same address value as arrayInB, therefore they will point to the same object. This is because instance variables use 'assign' setter semantics.
You can always check to make sure by printing the pointer addresses: