在 ViewController 之间传递 NSArray。是通过指针还是通过复制?

发布于 2025-01-04 15:56:32 字数 681 浏览 4 评论 0原文

给定两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人 2025-01-11 15:56:32

在您的代码中,arrayInB 指向与 arrayFromA 相同的内存地址,而 arrayFromA 指向 arrayInA。我建议更改您的代码,以便 arrayInB 保留对其接收的数组的引用。

if (self){
   arrayInB = [arrayFromA retain];
}

或者

@property (nonatomic,retain) NSArray * arrayInB;

if (self){
   self.arrayInB = arrayFromA;
}

如果您希望 arrayInB 成为其自己的数组,指向与 arrayFromA 相同的对象,那么您应该这样做。

if(self){
   self.arrayInB = [NSArray arrayWithArray: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.

if (self){
   arrayInB = [arrayFromA retain];
}

or

@property (nonatomic,retain) NSArray * arrayInB;

if (self){
   self.arrayInB = arrayFromA;
}

If you want arrayInB to be its own array that points to the same objects as arrayFromA then you should do this.

if(self){
   self.arrayInB = [NSArray arrayWithArray:arrayFromA];
}
鲜血染红嫁衣 2025-01-11 15:56:32

由于您只创建了一个数组(该代码未显示,但由您的注释“假设所有 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.

叹倦 2025-01-11 15:56:32

根据您提供的信息,arrayInA 应该与 arrayInB 具有相同的地址值,因此它们将指向同一对象。这是因为实例变量使用“分配”设置器语义。

您始终可以通过打印指针地址进行检查以确保:

- (id) initWithArray:(NSArray*)arrayFromA {
    ...
    if (self) {
      arrayInB = arrayFromA;
      NSLog(@"arrayInA:%x arrayInB:%x", arrayInA, arrayInB);
      ...
    }
    return self;
}

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:

- (id) initWithArray:(NSArray*)arrayFromA {
    ...
    if (self) {
      arrayInB = arrayFromA;
      NSLog(@"arrayInA:%x arrayInB:%x", arrayInA, arrayInB);
      ...
    }
    return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文