根据 ABPersonGetSortOrdering() 的结果,我想通过名字或姓氏对 UILocalizedIndexCollation 进行排序。
我在切换 collationStringSelector 参数所使用的 @selector 上遇到了问题。
很容易就可以写出冗长的代码:
NSArray *sortedSubarray; if (ABPersonGetSortOrdering() == 0) { sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:@selector(fname)]; } else { sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:@selector(lname)]; }
我尝试过类似这样的代码但没有成功:
SEL sorter = ABPersonGetSortOrdering() == 0 ? NSSelectorFromString(@"fname") : NSSelectorFromString(@"lname"); sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:@selector(sorter)];
我还尝试了其他想法,但似乎都没有起作用。
是否有更好的方法来动态传递选择器名称呢?
我在切换 collationStringSelector 参数所使用的 @selector 上遇到了问题。
很容易就可以写出冗长的代码:
NSArray *sortedSubarray; if (ABPersonGetSortOrdering() == 0) { sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:@selector(fname)]; } else { sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:@selector(lname)]; }
我尝试过类似这样的代码但没有成功:
SEL sorter = ABPersonGetSortOrdering() == 0 ? NSSelectorFromString(@"fname") : NSSelectorFromString(@"lname"); sortedSubarray = [collation sortedArrayFromArray:[sections objectAtIndex:section] collationStringSelector:@selector(sorter)];
我还尝试了其他想法,但似乎都没有起作用。
是否有更好的方法来动态传递选择器名称呢?