NSMutableDictionary setObject:forKey 当值为 NSMutableArray 时不起作用
dict 是 NSMutableDictionary; array 是 NSMutableArray 并且不为零;
代码:
[dict setObject:array forKey:@"key"];
没有错误或警告,但字典为空,没有设置或添加任何内容。
当我使用下面的代码时,它有效:
[dict setObject:[NSArray arrayWithArray:array] forKey:@"Key"];
任何人都可以告诉我为什么吗?
更新: dict
和 array
都是局部变量,并且已经初始化。dict
为 nil。
NSLog(@"%@", array)
打印了数组的值:
({"Title":"firstTitle","Date":"20110101"},{"Title":"secondTitle","Date":"20110102"})
更新:
我犯了一个错误。array
起初不为空,但我清空了其在后续操作中。
感谢@Bvarious。
dict is NSMutableDictionary; array is NSMutableArray and is NOT nil;
The code:
[dict setObject:array forKey:@"key"];
There is no error 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您知道您正在使用不同大小写的“key”,对吧?
@"Key"
与@"key"
不同。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?
该代码似乎是正确的。您的
数组
很可能是nil
。使用
arrayWithArray
时,插入的对象将是一个数组,即使其值本身设置为nil
。The code seems to be correct. Most probably your
array
isnil
.When using
arrayWithArray
, the inserted object will be an array, even if its value itself is set tonil
.