views:

98

answers:

4

So I have a property NSMutableArray *grades. At the only place where I set this property, I am doing this:

NSMutableArray *array = [[NSMutableArray alloc] init];
self.grades = array;    
[array release];
[self.grades addObject:@"20"];

The last statement generates an exception: -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'.

What in the world am I missing?

+6  A: 

Make sure grades is a NSMutableArray.

Edit:

copy returns an immutable copy, so you can't make changes. From Apple's Objective-C docs:

Copy

If you use the copy declaration attribute, you specify that a value is copied during assignment. If you synthesize the corresponding accessor, the synthesized method uses the copy method. This is useful for attributes such as string objects where there is a possibility that the new value passed in a setter may be mutable (for example, an instance of NSMutableString) and you want to ensure that your object has its own private immutable copy. For example, if you declare a property as follows:

Although this works well for strings, it may present a problem if the attribute is a collection such as an array or a set. Typically you want such collections to be mutable, but the copy method returns an immutable version of the collection. In this situation, you have to provide your own implementation of the setter method, as illustrated in the following example.

Copying the entire collection on assignment is a heavy operation. Are you sure you don't want to retain the collection, or just assign it?

If you really want a mutable copy, then write your own setter as the docs suggest.

- (void)setGrades:(NSMutableArray *)array {
    // make shallow/deep copy here, and assign to `grades`, not `self.grades`
}
Anurag
It is: `@property (nonatomic, copy) NSMutableArray *grades;`
raheel
change it to `(nonatomic, retain)`
djhworld
why do you need to copy it in the first place? From the code in the original post you allocated the array and passed it to the instance variable `grades`, then you released the original object. This is the same behaviour as just using a retain call.
djhworld
A: 

What is grades declared as?

From the looks of the error message your declaring grades as an NSArray and while this is valid it does mean that you lose the mutability of the array.

To maintain the array as mutable you'll need to declare grades as an NSMutableArray as well.

edit:

In light of your edit the reason could be that your using the copy keyword in the property, this would mean that when your assigning the array using self.grades the synthesised setter method makes an immutable copy of array

James Raybould
The class `grades` is statically declared as makes no difference to what the object actually is. It will not make it immutable. You'd get a warning at compile time, but definitely not a runtime error.
Chuck
+8  A: 

It sounds like the property is set to copy, which means the synthesized accessor makes an immutable copy of the array

Chuck
A: 

self.grades probably returns an NSArray if declared as @property NSArray* grades seeing this the compiler freaks and does not want to support addObject: method. You have 2 options

  1. cast it like [(NSMutableArray*)self.grades addObject:].
  2. add the object before assigning the array.
This is a runtime exception. The compiler would merely produce a warning.
Chuck