2011-08-30 7 views
0

나는 comparer selector를 사용하고있는 NSMutableArray를 정렬하려고합니다.NSMutableArray를 어떻게 정렬합니까?

- (NSComparisonResult)compare:(Product *)someObject { 
    return [self.Price compare:someObject.Price]; 
} 

HomeProducts = [HomeProducts sortedArrayUsingSelector:@selector(compare:)]; 

같은 배열을 정렬하고 다른 배열에 할당하지 않으려합니다.

request for member 'Price' in something not a structure or union .... 

자아를 인식하지 못합니다.

+0

NSMutableArray에 어떤 객체를 저장합니까? PLZ가 동일한 코드를 제공 할 수 있습니까? –

+0

이 이 하나를 시도 http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it ... – shivangi

+0

에 http : // 유래 .COM/질문/805547/방법 - 투 - 정렬 AN-있는 NSMutableArray -와 - 사용자 정의 개체 -에 - 그것은 이 링크는 당신을하는 데 도움이 될 수있다 참조 ... – shivangi

답변

1

클래스에있는 compare:이라고 가정하면 콘솔을 사용하여 더 자세히 조사해야합니다. 예외가 발생하면

, selfsomeObject의 값이 무엇인가 - 나는 그들 중 하나가 출시 된 것 중 하나가 아닌 Product 또는 무언가 (! 나는 후자의 생각)

는 충돌이 발생 것을 내기,

po self 
po someObject 

를 (또는 콘솔의 왼쪽에있는 GUI를 사용) :이 같은 콘솔에서 자신과 여기서 someObject의 값을 확인할 수 있습니다.


편집 답변 댓글을 :

sortedArrayUsingSelector:가있는 NSArray 반환 HomeProducts가있는 NSMutableArray이다. 대신보십시오 : 당신이 그것을 사용하는 방법

HomeProducts = [[HomeProducts sortedArrayUsingSelector:@selector(compare:)] mutableCopy]; 
+0

나는 compare : Product 클래스에 넣었고 그 오류는 제거되었지만 예외를 발생시키는 또 다른 경고는 다음과 같다. 경고 : 호환되지 않는 Objective-C types 'struct NSArray *', 'struct NSMutableArray *'를 예상했다. – Azhar

+0

내 편집 내용보기 :) – deanWombourne

1

compare:Product하는 방법이어야한다. Price 간단한 유형, 사용하는 경우,

[homeProducts sortUsingComparator: ^NSComparisonResult (id obj1, id obj2) 
{ 
    return [[obj1 Price] compare: [obj2 Price]]; 
} 

또는 : 대신 블록을 사용해보십시오

[homeProducts sortUsingComparator: ^NSComparisonResult (id obj1, id obj2) 
{ 
    return ([obj1 Price] < [obj2 Price]) ? NSOrderedAscending : 
      ([obj1 Price] > [obj2 Price]) ? NSOrderedDescending : 
              NSOrderedSame; 
} 

FWIW,는 등의 이름 인스턴스 변수, 변수, 속성을주는 것이 관례입니다 소문자로 시작하고 유형은 대문자로 시작합니다.

관련 문제