Home > Coding > NSCFArray append string errors, distinct Objective-C types

NSCFArray append string errors, distinct Objective-C types

January 22nd, 2009

Picture 1.png

Ran into some weirdness earlier which was absolutely killing me. I’m building a project using SQLite Persistent Objects, and this:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
	dict = [Commands sortedFieldValuesWithKeysForProperty:@"commandname"];
	self.keyLookup = [dict mutableDeepCopy];

	NSMutableArray *array = [[NSMutableArray alloc] init];
	array = [[[self.keyLookup allValues] sortedArrayUsingSelector:@selector(compare:)] retain];

was throwing an error about assigning the values from a distinct Objective-C type. I was able to fake my way around it in another viewController I had a similar issue with, but this time it was a show stopper because when I tried to work with the resulting array later (by adding/removing some stuff from it) I would get another error:

exception: *** -[NSCFArray addObject:]: mutating method sent to immutable object

Well, I was pretty dammed sure I hadn’t asked for my array to be immutable, but I’m guessing somewhere in the code for the persistent object (which I’ll be checking into) there’s a copy happening (or something similar) and its turning the returned data into an NSCFArray. To fix it… I needed to make it a mutable array again:

if (self.commandArray != array){
		[self.commandArray release];
		self.commandArray = [array mutableCopy];

Of course this may be simple if you’re the programming type… but it drove me nuts for a while!

Post to Twitter Tweet This Post

brandon Coding

  1. No comments yet.

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.