Search code examples
objective-csortingnsarray

Ios error while sorting an NSArray


When I try to sort an array, I got an exception like below:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key levels.'

To sort the arrays I use the following lines:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"levels" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];

NSArray *reversedLevelIdArray = [levelsDictionary.allKeys sortedArrayUsingDescriptors:descriptors];

The levelsDictionary.allKeys contains the values like below:

( level2, level1 )

What is wrong with my code?


Solution

  • Just use the following codes to sort the array, instead of all of your other line of codes..

    NSArray *reversedIdArray = [levelsDictionary.allKeys sortedArrayUsingDescriptors:@selector(caseInsensitiveCompare:)];