当我有这样的代码:

Fruit *fruit= [[Fruit alloc] init];

// This is effectively two different things right, one is "fruit" - the pointer, and another thing is the object it is pointing to (the one that was allocated using alloc/init) - though not directly visible

当我将此添加到NSArray时:
[myArray addObject: fruit];

实际上添加到数组的是指向Fruit类对象的指针,对吗?

最佳答案

是的,该指针的副本指向有效的初始化对象,因此以下内容不会引起问题(至少在ARC下):

Fruit *fruit= [[Fruit alloc] init];
[myArray addObject: fruit];
fruit = nil;     // OK, array still contains a valid Fruit object

关于ios - objective-c 中的NSArray-它包含什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21256524/

10-13 04:00