2011-03-17 2 views
4

ABPersonGetSortOrdering()의 결과에 따라 UILocalizedIndexCollation을 이름 또는 성으로 정렬하려고합니다.@selector를 동적으로 전달

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)]; 

나뿐만 아니라 다른 아이디어를 시도했다 , 아무 것도 작동하지 않는 것 같습니다.

선택자 이름을 동적으로 전달하는 더 좋은 방법이 있습니까?

답변

7
당신은 거의가

, 단지 주변 sorter에서 @selector() 부품을 제거 :

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

최고! 나는 질문을하지 않기 위해 가장 열심히 노력했다. .. 최소한 나는 가까웠다! 빠른 답변 감사합니다. – djibouti33

관련 문제