Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在编写一个NSArray类别以包含-objectAtRandom从随机索引返回对象的消息(类似于 Python 的选择)。
NSArray
-objectAtRandom
我应该在返回之前自动释放这个对象吗?我相信我不应该,但我不确定......
根据正常的内存管理规则,不,你不应该。由于您可能objectAtIndex:用于返回对象,因此您不需要自己进行任何内存管理。
objectAtIndex:
我会写return [[object retain] autorelease]- 这将保证,即使数组将被释放,用户也将能够使用对象,直到当前的 runloop 循环完成。
return [[object retain] autorelease]