开发者

Passing a @selector dynamically

开发者 https://www.devze.com 2023-02-17 03:57 出处:网络
Depending on the result from ABPersonGetSortOrdering(), I want to sort a UILocalizedIndexCollation by first name or last name.

Depending on the result from ABPersonGetSortOrdering(), I want to sort a UILocalizedIndexCollation by first name or last name.

I'm having problems switching the @selector used for the collationStringSelector parameter.

It'd be very easy to just write this verbosely:

NSArray *sortedSubarray;
if (ABPersonGetSortOrdering() == 0) {
    sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:@selector(fname)];
} else {
    sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:@selector(lname)];
}

I've tried something like this with no luck:

SEL sorter = ABPersonGetSortOrdering() == 0 ? NSSelectorFromString(@"fname") : NSSelectorFromString(@"lname");
sortedSubarray = [collation sortedArrayFromArray:[sectio开发者_如何学Pythonns objectAtIndex:section] collationStringSelector:@selector(sorter)];

I've tried out other ideas as well, and nothing seems to be working.

Is there a better way to pass a selector name dynamically?


You are almost there, just remove the @selector() part from around sorter:

sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:sorter];
0

精彩评论

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