Apple 提供了这个示例:
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error
}
为什么他们在这里调用 mutableCopy?是因为他们想要一个 NSMutableArray 而不是 NSArray,以便稍后编辑/更改它吗?还是还有其他原因?
最佳答案
答案可以在 article 中进一步找到。这提供了您的示例:
When the table view controller loads its view, it should fetch the Event objects and keep them in the events array so that they can be displayed later. The events array needs to be mutable since the user can add and remove events.
示例中的代码块是从持久存储中获取托管对象的多部分过程的一部分。该过程的下一步将调用 setEventsArray:
,这需要一个可变数组。
关于iphone - 为什么我必须制作该数组的可变副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2133551/