当我有这样的代码时:
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类对象的指针,对吗?
答案 0 :(得分:2)
是的,指针的副本,指向有效的初始化对象,因此以下不会导致问题(至少在ARC下):
Fruit *fruit= [[Fruit alloc] init];
[myArray addObject: fruit];
fruit = nil; // OK, array still contains a valid Fruit object
答案 1 :(得分:0)
是的,它会为您的对象添加一个强大的指针。
使用ARC时请记住,当没有强指针时,会自动释放对象。这意味着,如果您使用ARC,则需要设置任何指向您必须“nil”的对象的指针才能释放它。这包括例如存储在NSArray中的指针。
答案 2 :(得分:0)
如你所知,* var是指针的值,那么var
是指针(如c语言)
这意味着如果* fruit是对象,那么您将指针fruit
添加到数组中,如[myArray addObject: fruit];