NSMutableDictionary setObject:forKey did't work when value is NSMutableArray
dict is NSMutableDictionary; array is NSMutableArray and is NOT nil;
The code:
[dict setObject:array forKey:@"key"];
There is no e开发者_如何学Crror or warning but the dict is null,nothing is set or added.
When I use the code below,it works:
[dict setObject:[NSArray arrayWithArray:array] forKey:@"Key"];
Can anyone tell me why ?
Update:
Both dict
and array
are local variables and have been initialized.The dict
is nil.
NSLog(@"%@", array)
has printed the value of array:
({"Title":"firstTitle","Date":"20110101"},{"Title":"secondTitle","Date":"20110102"})
UPDATE:
I have made a mistake.The array
is not null at first,but I emptied it in follow operation.
Thanks for @Bavarious.
You're aware that you're using different capitalizations of "key", right? @"Key"
is not the same thing as @"key"
.
I think you have not initialized array in 1st statement. check if its allocated memory or not?
The code seems to be correct. Most probably your array
is nil
.
When using arrayWithArray
, the inserted object will be an array, even if its value itself is set to nil
.
精彩评论