2011-03-17 8 views
4

ABPersonGetSortOrdering()の結果に応じて、姓または名でUILocalizedIndexCollat​​ionを並べ替える必要があります。@セレクタを動的に渡す

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

関連する問題