IOS从NSArray获取对象

hiw*_*dls 1 memory-management objective-c nsarray ios

我正在开发一个IOS应用程序.如何从NSArray列表中获取对象.避免出现内存错误的最佳方法是什么?你能告诉我正确的方法吗?谢谢

建议1

Person *person = [[[Person alloc] init] autorelease];
        person = [self.userFavourites objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

建议2

Person *person = [self.userFavourites objectAtIndex:0];
[person retain];

//Make the required action

[person release];
Run Code Online (Sandbox Code Playgroud)

建议3

gra*_*ver 5

Person *person = [self.userFavourites objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

userFavourites阵列将保留所有元素中,当你得到一个元素,配备自动释放.

编辑:
建议1 - 分配init自动释放一个Person对象然后从数组中获取不同的人是没有意义的.
建议2 - 您不需要保留对象,因为数组会保留它.如果您需要在范围之外,您只需要保留它