2011-03-17 61 views
4

根据ABPersonGetSortOrdering()的结果,我想按名字或姓氏对UILocalizedIndexCollat​​ion进行排序。动态传递@selector

我在切换用于collat​​ionStringSelector参数的@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)]; 

我已经尝试了其他的想法,似乎没有任何工作。

有没有更好的方式来动态地传递选择器名称?

回答

7

你几乎那里,只是从周围sorter删除@selector()部分:

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

真棒!我尽力不要问这个问题......至少我接近了!感谢您的快速回复。 – djibouti33 2011-03-17 07:06:37