2012-04-21 5 views
2

NSString을 NSMutable 배열로 토큰 화했습니다. 그런 다음 NSSortDescriptor를 사용하여 배열의 길이만큼 문자열을 정렬 할 수 있습니다. 그런 다음 가장 긴 단어부터 시작하여 특정 단어에 대한 방법을 수행 할 수 있습니다.NSMutableArray의 원래 정렬 순서를 복원하는 방법은 무엇입니까?

여기가 내 문제의 원인입니다. 배열의 원래 정렬 순서를 복원하여 배열을 편집 된 문자열로 다시 실행할 수 있어야합니다.

답변

2

내 대답은 근본적으로 배열을 유지하고, 정렬 된 복사본을 만들고, 처리 된 정렬 된 복사본을 단계별로 처리 한 다음 멀리 던져 버리는 것을 제외하고는 기본적으로 스티브와 같습니다. 그래서 예.

NSArray *yourOriginalArray = ...whatever...; 

// do processing 
for(id object in [yourOriginalArray sortedArrayUsing<whatever means>]) 
{ 
    // if you're doing processing that doesn't change the identity of 
    // the object then there's no need to worry about this, but supposing 
    // you want to adjust the identity (eg, the source array is full of 
    // immutable objects and you want to replace them) then... 

    NSUInteger originalIndex = [yourOriginalArray indexOfObject:object]; 

    id replacementObject = [self mutatedFormOf:object]; 
    [yourOriginalArray replaceObjectAtIndex:originalIndex 
        withObject:replacementObject]; 
    // of course, this is only if yourOriginalArray is mutable 
} 
+0

고마워, 내 생각 엔이게 내가 내 장애물을 극복해야 할 필요가있는 것 같아. – noahread

2

NSMutableArray는 이전 상태에 대해서만 알지 못합니다. 현재 상태입니다. 아마 더 우아한 해결책이있을 수 있지만 당신이 할 수있는 한 가지는 원래의 정렬 순서를 저장하는 또 다른 배열을 만드는 것입니다.

관련 문제