BluGeni
BluGeni

Reputation: 3454

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];

Upvotes: 6

Views: 16575

Answers (1)

trojanfoe
trojanfoe

Reputation: 122458

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.

Upvotes: 22

Related Questions