iphone - 为什么我必须制作该数组的可变副本?

标签 iphone cocoa cocoa-touch core-data macos

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/

相关文章:

objective-c - 比较两个日期

iphone - 如何使 UITableViewCell 突出显示状态持续存在

cocoa - 为什么我的观点没有出现?

swift - 使用可信服务器验证 IAP 收据。如果返回的 JSON 不包含 "latest_receipt_info" key ,如何确定试用资格?

objective-c - 在 Swift/Objective-C 中监听窗口调整大小事件

objective-c - 如何获取单选按钮的状态以及是否被选中?

objective-c - iOS 应用程序会自动检查新版本吗?

iOS - [UIView setAnimationDidStopSelector :@selector(animationDidStop:finished:context:)] called too soon

ios - 推送通知未在后台接收但在前台工作

c# - 使用 C# 编写代码并构建成跨平台二进制文件? (iOS 和 Android)