Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setValue:forUndefinedKey this class is not key value coding-compliant

I have an nsmutablearray that looks like this:

(
        {
        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";
    }
)

And im trying to set the value of caption like this:

[[[self.form valueForKey:@"photos"] objectAtIndex:indexPath.row] setValue:@"hi" forKey:@"caption"];

([self.form valueForKey:@"photos"] is the array)

but I get :

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0xa68ec40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key caption.'

EDIT:

If I use setObject forKey I get:

-[__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'

How do I fix this?

CORRECT CODE TO FIX THIS:

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];
like image 459
BluGeni Avatar asked Nov 19 '13 15:11

BluGeni


1 Answers

The reason you are getting the exception is because you have an array of NSDictionary objects, which don't respond to setObject:forKey: (or setValue:forKey:).

You probably want to convert them all to NSMutableDictionary objects as soon as you receive them.

like image 157
trojanfoe Avatar answered Nov 15 '22 04:11

trojanfoe