NSArray Mutability Confusion


#1

I have a class ‘Order’, for which I have declared the following property with synthesized accessor methods:

@property (nonatomic, copy) NSMutableArray *lineItems;

@synthesize lineItems = lineItems_;

In another class method I perform the following:

foundOrder = [[Order alloc] init];
                
NSLog(@"Got %@", [[self getOrderLineItemsForOrderPK:orderPK] class]);
                
[foundOrder setLineItems:[self getOrderLineItemsForOrderPK:orderPK]];
                
NSLog(@"Output %@", [[foundOrder lineItems] class]);

The results of which are:

Got __NSArrayM
Output __NSArrayI

Why is my getter returning an immutable array when I set with a mutable?


#2

Never mind. I just realized looking at my post that the ‘copy’ directive of my property declaration was causing the immutable copy. After setting that property to ‘strong’, I maintain mutability.


#3

This raises a point worth clarifying for posterity:

The NSCopying protocol defines -copy and -mutableCopy methods. On a mutable object, vanilla -copy should always return an immutable copy.