Reputation: 4409
(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?
Upvotes: 4
Views: 1567
Reputation: 35616
No your conclusion is not right. If the objectForKey
returns nil
then you 're sending a mutableCopy
message to nil
which returns of course nil
(every message sent to nil
in ObjC returns nil
) and that's why you' re seeing the else
part. (The Array is Not Mutable
message is misleading since you do not even have an NSArray
instance at this point).
Upvotes: 3