开发者

Need help with sorting NSMutableArray with custom objects

sorry to bother you with this question, but after hours of googleing and browsing here, I still cannot figure out, what's the problem.

The thing is, I create a NSMutableArray with custom objects of type 'SearchResult'. The SearchResult class is still pretty basic:

@interface SearchResult : NSObject {

    NSString *_id;
    NSString *_title;
    NSNumber *_year;
}

@property (nonatomic, copy) NSString *id;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSNumber *year;

@end

here is the code, where the (bad) magic happens:

if (self.sResults == nil) {
    self.sResults = [[NSMutableArray alloc] init];
}
else if ([self.sResults count] > 0){
    [self.sResults removeAllObjects];
}
//JsonConverter uses JsonTouchKit for parsing a json string
JsonConverter *_converter = [[JsonConverter alloc] init];
NSDictionary *parsedResults = [_converter getParsedObjectFromData:results];

for (NSDictionary *movieObj in [parsedResults objectForKey:@"results"]) {
    SearchResult *movie = [_converter newSearchResultObjectFromSearchJsonObject:movieObj];
    [self.sResults addObject:movie];
}
[_c开发者_运维问答onverter release];
//sort the results
NSSortDescriptor *desc = [[[NSSortDescriptor alloc] initWithKey:@"year" ascending:NO] autorelease];

NSArray * sortDescriptors = [NSArray arrayWithObject:desc];
[self.sResults sortUsingDescriptors:sortDescriptors];  //<-- HERE!!!
[self.tableView reloadData];

The thing is, if I get only a few results, e.g. 3, everything is fine and the array is sorted correctly. BUT as soon as I get some more results, e.g. 18, the app crashes on the marked line with a 'SIGABRT' and the below stack. If I inspect the given adress "0x5c31470" with my little knowledge of gdb commands I get the following:

po 0x5c31470 -> 2004 //yes, thats the year it should use for sorting
whatis 0x5c31470 -> type = int

Stack track:

2011-09-13 12:30:10.352 VideoCatalogue[1294:207] -[NSCFNumber length]: unrecognized selector sent to instance 0x5c31470

2011-09-13 12:30:10.353 VideoCatalogue[1294:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber length]: unrecognized selector sent to instance 0x5c31470'

Has anyone any idea, how I can solve this? Thank very much!


Use this approach:NSArray Class Reference Look for the explanation of: sortedArrayUsingFunction:context:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜