2011-10-17 2 views

답변

12

당신은 항목을 제거하고 올바른 공간에 삽입합니다 :

id tmp=[[animals objectAtIndex:2] retain]; 
[animals removeObjectAtIndex:2]; 
[animals insertObject:tmp atIndex:0]; 
[tmp release]; 

을 당신은 객체를 유지하거나 당신이 그것을 제거하기 위해 배열을 말할 때, 그것은 개체를 해제합니다. 인덱스를 모르는 경우

은 당신이 뭔가를 할 수 있습니다 :

NSMutableArray* animals = [NSMutableArray arrayWithObjects:@"cat", @"lion", @"dog", @"tiger",nil]; 

for (NSString* obj in [[animals copy] autorelease]) { 
    if ([obj isEqualToString:@"dog"]) { 
     NSString* tmp = [obj retain]; 
     [animals removeObject:tmp]; 
     [animals insertObject:tmp atIndex:0]; 
     break; 
    } 
} 
이 방법은 모든 목록에 가서 "개"를 검색하고, 발견 된 경우가에서 제거 할 것

+0

문자열 대신에? – HardCode

+0

@HardCode 네, 루프를 찾으면됩니다. 그 대답을 반영하여 답변을 업데이트하겠습니다. – utahwithak

+0

감사합니다. – HardCode

3

기존 요소 (예 : 개)를 제거한 다음 배열의 시작 부분에 다시 삽입 할 수 있습니다.

NSMutableArray *animals = [NSMutableArray arrayWithObjects:@"cat", @"lion", @"dog", @"tiger",nil]; 
NSString *dog = @"dog"; 
// Check to see if dog is in animals 
if ([animals containsObject:dog]) { 
    // Remove dog from animals and reinsert 
    // at the beginning of animals 
    [animals removeObject:dog]; 
    [animals insertObject:dog atIndex:0]; 
} 
+0

그것은 addObject에서 오류를 제공합니다. 오류 : 찾을 수없는 반환 유형의 기본값은 – HardCode

+0

입니다. 죄송합니다. 메소드의 이름은 insertObject : atIndex입니다. 나는 코어가 내 대답을 테드 –

+0

멋지게 잘 작동합니다! – HardCode

6

나는 비효율적이라고 지적하고 싶습니다. NSMutableArray에서 객체를 제거/삽입하면 삭제/삽입 후 모든 행에 잠재적으로 영향을 미칩니다. Apple이 그들의 가변 배열을 유지하기 위해 사용하는 내부적 방법이 명확하지 않기 때문에 '잠재적으로'라고 말합니다. 그러나 단순한 c- 배열이라면 삭제/삽입 인덱스 이후의 모든 행을 아래로/위로 이동해야합니다. 매우 큰 배열에서는 이동 된 항목이 처음에있는 경우 비효율적 일 수 있습니다. 그러나 배열의 항목을 대체하는 것은 비효율적이지 않습니다. 따라서 다음이있는 NSMutableArray의 카테고리가 (이 코드는 ARC 아래주의, 그래서 더 메모리 관리) : 또한

- (void) moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex{ 
    if (fromIndex == toIndex) return; 
    if (fromIndex >= self.count) return; //there is no object to move, return 
    if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end 
    id movingObject = [self objectAtIndex:fromIndex]; 

    if (fromIndex < toIndex){ 
     for (int i = fromIndex; i <= toIndex; i++){ 
      [self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : [self objectAtIndex:i + 1]]; 
     } 
    } else { 
     id cObject; 
     id prevObject; 
     for (int i = toIndex; i <= fromIndex; i++){ 
      cObject = [self objectAtIndex:i]; 
      [self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : prevObject]; 
      prevObject = cObject; 
     } 
    } 
} 

, 작은 보너스를 추가 기능을 높이기 위해, 당신은 항목에 대한 작업을 수행하는 경우 이동 (db 또는 뭔가를 업데이트하는 것처럼), 다음 코드는 매우 유용했습니다.

- (void) moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex withBlock:(void (^)(id, NSUInteger))block{ 
    if (fromIndex == toIndex) return; 
    if (fromIndex >= self.count) return; //there is no object to move, return 
    if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end 
    id movingObject = [self objectAtIndex:fromIndex]; 
    id replacementObject; 

    if (fromIndex < toIndex){ 
     for (int i = fromIndex; i <= toIndex; i++){ 
      replacementObject = (i == toIndex) ? movingObject : [self objectAtIndex:i + 1]; 
      [self replaceObjectAtIndex:i withObject:replacementObject]; 
      if (block) block(replacementObject, i); 
     } 
    } else { 
     id cObject; 
     id prevObject; 
     for (int i = toIndex; i <= fromIndex; i++){ 
      cObject = [self objectAtIndex:i]; 
      replacementObject = (i == toIndex) ? movingObject : prevObject; 
      [self replaceObjectAtIndex:i withObject:replacementObject]; 
      prevObject = cObject; 
      if (block) block(replacementObject, i); 
     } 
    } 
} 
+0

메서드는 O (n)처럼 보이지만 O (1)이어야합니다. – OpenThread

관련 문제