开发者

NSArray sortedArrayUsingSelector keeps erroring with SIGBART

EDIT: Are you even able to use a custom selector wit开发者_运维技巧h "sortedArrayUsingSelector"? I got this to work using [[ComicArray valueForKey:@"Name"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

The Exception "Does not recognize selector" is thrown. Anyone know how to sort this? The ComicArray is filled with ComicDB which is a custom class that contains .Name. I am trying to sort based on ComicDB.Name.

//atempt1
NSArray * comicArray2 = [[ComicArray valueForKeyPath:@"ComicDB.Name"] sortedArrayUsingSelector:@selector(compareByName:)];

//atempt2       
NSArray * compicArray2 = [ComicArray sortedArrayUsingSelector:@selector(compareByName:)];

- (NSComparisonResult)compareByName:(id)other
{
    return [[self valueForKeyPath: @"ComicDB.Name"] caseInsensitiveCompare:[other valueForKeyPath: @"ComicDB.Name"]];
}

Thanks, -Mike


sortedArrayUsingSelector: performs the selector on the objects in the array to determine their order. The compareByName: method should be in the ComicDB class. The following implementation will work if you have accessors defined. If you don't, change Name to valueForKey:@"Name".

- (NSComparisonResult)compareByName:(ComicDB *)other {
    return [[self name] caseInsensitiveCompare:[other name]];
}


Jan 23 2011 I could not get this to work. Instead i used the iOS 4.0 sort method:

NSArray * myArraySorted = [ComicArray sortedArrayUsingComparator:^(id obj1, id obj2)
                          {
                              return [[obj1 valueForKey:@"Name"] caseInsensitiveCompare: [obj2 valueForKey: @"Name"]]; 
                              // return [[obj1 Name] caseInsensitiveCompare: [obj2 Name]];
                          }];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜