2013-01-17 3 views
-1

배열이 있고 배열을 반복하면서 항목을 바꾸기를 원했습니다. 이럴 수는 없나요? 코드는 다음과 같습니다.동시에 항목 반복 및 바꾸기

for (int i = 0; i < [highlightItemsArray count]; i++){ 
    //replace the from array with the new one 
    NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithDictionary:highlightItem]; 
    [tempDictionary setObject:[newHighlightItem objectForKey:@"from"] forKey:@"from"]; 
    [highlightItemsArray replaceObjectAtIndex:i withObject:tempDictionary]; 
    [indexes addIndex:i]; 
} 

객관적으로 C가 정당한 것인지 알고 싶습니까? 그렇지 않다면 그렇게하는 대안은 무엇입니까?

+0

같은 그것은이 경우에만 실패 어떤 충돌 –

+2

가 발생하지 않습니다 당신은 '빠른 열거 형'(배열 {}에서 id 객체에 대해)을 사용하고있었습니다. 자신이하는 일을 성취하기위한 간단한 방법이있을 수 있지만, 정확히 무엇을 달성하려고하는지 확신 할 수 없습니다. – MaxGabriel

답변

0

코드가 위를 참조하십시오.

오류가 발생한 경우 이는 배열을 변경 한 것이 아닙니다.

그러나 당신이이 1 initialiser을 변경할 수 있습니다 (하지만이 한 번만 실행) ((;), dowhile가있는 동안) 이전 스타일의 루프 2. 카운터 3. 조건이다.

그러나 빠른 열거 형의 경우에는이 모든 것을 수행 할 수 없습니다.

따라서 위의 코드를 (... in ...)와 통합하면 변경 불가능한 객체를 변경하려고 시도하는 중 오류가 발생합니다. 카운터 및/또는 배열을 정의하더라도 immutable fast enumeration은 immutable으로 처리합니다.

0

빠르고 즉 그 것이다 배열 복사를 해결하기 더러운 방법,

NSMutableArray *higlightItemsCopy = [highlightItemsArray mutableCopy]; 
for (int i = 0; i < [highlightItemsArray count]; i++){ 
    //replace the from array with the new one 
    NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithDictionary:highlightItem]; 
    [tempDictionary setObject:[newHighlightItem objectForKey:@"from"] forKey:@"from"]; 
    higlightItemsCopy[i] = tempDictionary; 
    [indexes addIndex:i]; 
} 
highlightItemsArray = higlightItemsCopy; 

그것을 테스트하지 않은,하지만 그