我无法理解这两段代码之间的区别,因为它们的行为方式相同。
- (NSArray *)handleImages
{
NSMutableArray *images = [NSMutableArray new];
//...Do something with the array..
return [images copy];
}
- (NSArray *)handleImages
{
NSMutableArray *images = [NSMutableArray new];
//...Do something with the array..
return images;
}
最佳答案
该方法设置为返回一个不可变数组。第一个版本在返回之前将它创建的可变数组转换为不可变数组。第二个版本只是返回可变数组。第一个版本稍微更干净,因为调用者需要一个不可变数组。
关于ios - 返回对象的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24016339/