tags:

views:

112

answers:

1

I'm trying to sort an NSSet of NSManagedObjects. The value for one of the set item's "pubDate" property is nil. The following code throws an exception [NSManagedObject valueForUndefinedKey] when it is sorted. How can I get it to ignore the element without a value for that key?

NSSortDescriptor *sortNameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"pubDate" ascending:NO] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortNameDescriptor, nil];
return [[self.items allObjects] sortedArrayUsingDescriptors:sortDescriptors];
A: 

You can't sort objects on a value that doesn't exist. Can you give it a default value? It's like trying to sort the array {1, 5, 3, X, 9, 7}, you just can't unless you tell it how to deal with X.

David Kanarek