setValue:forUndefinedKey此类不符合键值编码

时间:2013-11-19 15:49:12

标签: ios objective-c

我有一个看起来像这样的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];

1 个答案:

答案 0 :(得分:22)

您获得异常的原因是因为您有一组NSDictionary个对象,这些对象无法响应setObject:forKey:(或setValue:forKey:)。

您可能希望在收到它们后立即将它们全部转换为NSMutableDictionary个对象。