I have a custom plist:
<key>Ford</key>
<dict>
<key>name</key>
<string>Ford</string>
<key>color</key>
<string>green</string>
<key>price</key>
<integer>45000</integer>
</dict
<key>Toyota</key>
<dict>
<key>name</key>
<string>Toyota</string>
<key>color</key>
<string>blue</string>
<key>price</key>
<integer>40000</integer>
</dict
<key>BMW</key>
<dict>
<key>name</key>
<string>BMW</string>
<key>color</key>
<string>blue</string>
<key>price</key>
<integer>40000</integer>
</dict>
I also have an UITableView and I fill it with some cars from this plist.
NSEnumerator *enumerator = [dictionary objectEnumerator];
id returnValue;
while ((returnValue = [enumerator nextObject]))
{
if ([[returnValue valueForKey:@"color"]isEqualToString:@"blue")
{
[array addObject:[returnValue valueForKey:@"name"]];
}
Now, when my UITableView is full, I’d like to sort cars by price with NSSortDescriptor:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"price" ascending:YES];
[array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
But with this code my app crashes: reason: '[<__NSCFString 0x89a1710> valueForUndefinedKey:]: this class is not key value coding-compliant for the key price.'
Why does this happen ?
You say that you are filling your array with cars but you’re not. You are filling it with names of cars and those names are strings. The error message is telling you that strings don’t have a “price” attribute.