(I cant find this on the Net)
All code is within one object. In the .h file I define a NSMutableArray. And I alloc it
in an init.
-(id)init{
self = [super init];
if(self) {
definitionArray=[[NSMutableArray alloc] init];
[self initialLoadDefinitionData];
if([definitionArray isKindOfClass:[NSMutableArray class]]) {
NSLog(@"Array is Mutable");
} else {
NSLog(@"Array is Not Mutable");
}
}
return self;
}
I want to initialize based on NSUserDefaults values, so I call another function from the
init. This called initialLoadDefinitionData:theCollection is very basic.
-(void)loadDefinitionData{
definitionArray=[[[NSUserDefaults standardUserDefaults] objectForKey:@"SOMEKEY"] mutableCopy];
}
I have added the isKindOfClass in the init function just for debug purposes. The fun part
in this is that this IF statement returns MUTABLE when data could be fetched from the
NSUserDefaults. However if the @”SOMEKEY” was not available, the objectForKey returns a
nil and the definitionArray suddenly is IMMUTABLE. Even if the mutableCopy is specified.
So my conclusion is that if a objectForKey cannot find an object, the return value is
NOT MUTABLE. Is this conclusion correct?
Or can’t MutableCopy not address an object as mutable with it is Nil?
No your conclusion is not right. If the
objectForKeyreturnsnilthen you ‘re sending amutableCopymessage tonilwhich returns of coursenil(every message sent tonilin ObjC returnsnil) and that’s why you’ re seeing theelsepart. (TheArray is Not Mutablemessage is misleading since you do not even have anNSArrayinstance at this point).