简体   繁体   English

Objective-C:addObjectsFromArray是否复制对象?

[英]objective-c: does addObjectsFromArray copy objects?

Having a hard time figuring this one out... 很难弄清楚这个...

does addObjectsFromArray, a convenience method inside of NSArray copy everything, or does it keep the 'otherArray' parameter where it is in memory and do a LinkedList style point the tail to the 'otherArray' kind of move? 是NSObject内部的便捷方法addObjectsFromArray复制所有内容,还是将'otherArray'参数保留在内存中,LinkedList样式的尾部是否指向'otherArray'这种动作?

Im asking because I have pointers to important objects in some existing-array, but these pointers may point to a different object on an incoming-array. 我问是因为我有一些指向现有数组中重要对象的指针,但是这些指针可能指向传入数组上的另一个对象。

I re-point my important-object-pointer to some object within the incoming-array, and then call addObjectsFromArray on the existing-array with the incoming-array. 我将重要对象指针重新指向传入数组内的某个对象,然后使用传入数组在现有数组上调用addObjectsFromArray。

My worry is that my re-pointed important-object-pointers will point to nothing if the incoming-array is actually copied, when ARC decides to nil out the incoming-array. 我担心的是,当ARC决定取消传入数组时,如果实际上复制了传入数组,则我重新指向的重要对象指针将不指向任何内容。

I think you're overthinking this. 我认为您对此太想了。 You won't have a pointer "into" an NSArray. 您将没有“插入” NSArray的指针。 You'll have a pointer to an object, and the NSArray will also have a pointer to that object. 您将拥有一个指向对象的指针,并且NSArray也将具有指向该对象的指针。 Reassigning your pointer will not change the pointer in the NSArray. 重新分配指针不会更改NSArray中的指针。

As for copying, NSArray does not copy objects. 至于复制,NSArray不复制对象。 But as noted above, it does copy the pointer to the object, rather than holding a pointer-to-a-pointer or something weird like that. 但是如上所述,它确实将指针复制到对象,而不是持有指向指针的指针或类似的东西。 So in this code: 所以在这段代码中:

NSMutableString *a = [NSMutableString stringWithString:@"Cool"];
NSMutableString *b = a;
NSMutableArray *array = [NSMutableArray array];
[array addObject:a];
NSMutableString *c = [array objectAtIndex:0];
[b appendString:@" beans"];
a = nil;

At the end of running: 运行结束时:

  • b and c will be hold same NSMutableString — "Cool beans" — and mutating either of them would show up in the other one bc将具有相同的NSMutableString-“ Cool bean”-并且对它们中的任何一个进行突变都会在另一个中显示

  • The array holds the same NSMutableString as b and c — "Cool beans" — and accessing the string there will also show mutations to the string 该数组与bc拥有相同的NSMutableString(“酷豆”),访问那里的字符串也将显示该字符串的突变

  • a will be nil. a将为零。 This didn't affect any of the other pointers to the string when we reset it, because they point to the string, not the variable. 重置字符串时,这不会影响任何其他指向该字符串的指针,因为它们指向字符串,而不是变量。

When you addObjectsFromArray: , it is just as though you iterated over the array and added each individually. 当您添加addObjectsFromArray: ,就好像您遍历数组并单独添加每个数组一样。 The other array could go away at that point and it wouldn't matter, because your NSArray now has pointers to all the objects. 另一个数组此时可以消失,这没有关系,因为您的NSArray现在具有指向所有对象的指针。

I think that addObjectsFromArray works just like addObject : a pointer to the object is added to the array; 我认为addObjectsFromArray就像addObject一样addObject :将指向对象的指针添加到数组中; the object itself is not modified except for its retain count, which is incremented. 除了保留计数(递增)之外,该对象本身没有被修改。

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

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