문제

I have NSSet of NSStrings. I want to return alphabetically sorted array of NSSet elements with method – sortedArrayUsingDescriptors:.

I have read NSSortDescriptor documentation and still have no clue how to create one for simple NSString objects.

What key should I specify? Is there a better approach than using NSSortDescriptor?

도움이 되었습니까?

해결책

I can't find it documented that passing nil for the key is safe and means to use the object itself. So, I'd be hesitant to use it. You can use @"self" instead.

다른 팁

NSSet *sets = [NSSet setWithObjects:@"Peter", @"Brennon", @"Zach", @"Yan", @"Akila", nil];
NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sorted = [sets sortedArrayUsingDescriptors:@[desc]];

Logs out:

(
    Akila,
    Brennon,
    Peter,
    Yan,
    Zach
)

Just use nil as the key. E.g.

[set sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(compare:)]]]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top