【发布时间】:2013-12-03 06:24:51
【问题描述】:
我有一个看起来像这样的 nsmutablearray:
(
{
caption = "";
urlRep = "assets-library://asset/asset.JPG?id=6FC0C2DC-69BB-4FAD-9709-63E03182BEE1&ext=JPG";
},
{
caption = "";
urlRep = "assets-library://asset/asset.JPG?id=324E4377-0BCD-431C-8A57-535BC0FC44EB&ext=JPG";
}
)
我试图像这样设置标题的值:
[[[self.form valueForKey:@"photos"] objectAtIndex:indexPath.row] setValue:@"hi" forKey:@"caption"];
([self.form valueForKey:@"photos"] 是数组)
但我明白了:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0xa68ec40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key caption.'
编辑:
如果我使用 setObject forKey 我得到:
-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0xa6a88f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0xa6a88f0'
我该如何解决这个问题?
修正此问题的正确代码:
NSMutableDictionary *m = [[[self.form valueForKey:@"photos"] objectAtIndex:indexPath.row ] mutableCopy];
NSMutableArray *array = [self.form valueForKey:@"photos"];
[m setObject:textField.text forKey:@"caption"];
[array replaceObjectAtIndex:indexPath.row withObject:m];
【问题讨论】:
-
setObject:forKey:是做什么的? -
@trojanfoe 仍然崩溃,向问题添加了确切的错误。
标签: ios objective-c