I have a core data stack with 2 entities: ‘Client’ and ‘Car’. It is a one to may relationship. I have two tableViewControllers to display firstly the clients, and then once a client has been chosen a list of that clients cars.
I am currently using the following code fragment in my second tableViewController viewDidLoad method…
NSSet *cars = client.cars;
carsArray = [cars allObjects];
carsArray is a NSArray and is declared and synthesized in the second tableViewCotroller. If I try and make carsArray an NSMutableArray (so I can edit it later in the event a car is deleted) I get the following compiler warning…
Incompatible Objective-C types 'struct NSArray *', expected 'struct NSMutableArray *' when passing argument 1 of 'setCarsArray:' from distinct Objective-C type
Could someone explain to me why this works with a NSArray but not an NSMutableArray please?
Many thanks
allObjectsmethod returnsNSArray *and if you simply try to assign it to anNSMutableArrayproperty you are not converting the object itself.You need to use
+[NSMutableArray arrayWithArray:]or a more generic method-[NSObject mutableCopy]:This will be the case for all mutable classes in Objective-C: to create a mutable object from immutable you’ll have to explicitly call the conversion methods.