R – Why must I make an mutable copy of this array

cocoacocoa-touchcore-dataiphonemacos

Apple provided this example:

NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error
}

Why are they calling mutableCopy here? Is it because they wanted to have an NSMutableArray rather than an NSArray, to edit / change it later? Or is there another reason?

Best Answer

The answer can be found further up in the article that provided your example:

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.

The block of code in your example is one part of a multi-part process of fetching managed objects from a persistent store. The next step in the process will call setEventsArray:, which requires a mutable array.