2012-03-06 2 views
0

편집 : 나는 이것을 가지고이 배열에하지만 내 새로운 데이터의 새로운 datas를 둘 필요가replaceObjectAtIndex NSMutableArray를

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
    <dict> 
     <key>destinataire</key> 
     <string>david</string> 
     <key>expediteur</key> 
     <string>sophie</string> 
     <key>idMessage</key> 
     <string>1729</string> 
     <key>message</key> 
     <string>ok a plus</string> 
     <key>photoExp</key> 
     <string>http://photos/hbrJUlrTgj.jpg</string> 
     <key>statusE</key> 
     <string>1</string> 
    </dict> 
    <dict> 
     <key>destinataire</key> 
     <string>david</string> 
     <key>expediteur</key> 
     <string>max</string> 
     <key>idMessage</key> 
     <string>1730</string> 
     <key>message</key> 
     <string>ok a plus</string> 
     <key>photoExp</key> 
     <string>http:///photos/4xlWoAHT8b.jpg</string> 
     <key>statusE</key> 
     <string>1</string> 
    </dict> 
    <dict> 
     <key>destinataire</key> 
     <string>david</string> 
     <key>expediteur</key> 
     <string>michel</string> 
     <key>idMessage</key> 
     <string>1731</string> 
     <key>message</key> 
     <string>ok a plus</string> 
     <key>photoExp</key> 
     <string>http:///photos/TR7oO6O8Z8.jpg</string> 
     <key>statusE</key> 
     <string>1</string> 
    </dict> 
</array> 
</plist> 

: 내가 같이 어떻게 배열을

: 이것은 내 전체 코드입니다 : 소피 나의 새로운 데이터를 넣어 표시되는 위치를 내 배열의 인덱스를 찾을 수있다, 그래서

<dict> 
      <key>destinataire</key> 
      <string>david</string> 
      <key>expediteur</key> 
      <string>sophie</string> 
      <key>idMessage</key> 
      <string>1729</string> 
      <key>message</key> 
      <string>ok a plus</string> 
      <key>photoExp</key> 
      <string>http://photos/hbrJUlrTgj.jpg</string> 
      <key>statusE</key> 
      <string>1</string> 
     </dict> 

소피는 이미 내 배열에 존재한다

나는 그것이 작동하지만 내 모든 datas 모든 인덱스에 대체되었습니다 :

그래서 내가 중복 datas를 찾아 내가 replaceObjectAtindex를 사용하려고 할 때

if ([[allMessageArray valueForKey:@"expediteur"]containsObject:[dicoChat2 objectForKey:@"expediteur"]]) 
    { 
    for(int i=0;i<[allMessageArray count];i++) 
     { 
    NSDictionary *dicoChat22 = [allMessageArray objectAtIndex:i]; 

    NSMutableDictionary * datas2= [[NSMutableDictionary alloc]init]; 

    [datas2 setObject:[dicoChat22 objectForKey:@"destinataire"] forKey:@"destinataire"]; 
    [datas2 setObject:[dicoChat22 objectForKey:@"expediteur"] forKey:@"expediteur"]; 
    [datas2 setObject:[dicoChat22 objectForKey:@"photoExp"] forKey:@"photoExp"]; 
    [datas2 setObject:[dicoChat22 objectForKey:@"statusE"] forKey:@"statusE"]; 
    [datas2 setObject:[dicoChat22 objectForKey:@"idMessage"] forKey:@"idMessage"]; 

    [allMessageArray replaceObjectAtIndex:i withObject:datas2]; 
    [allMessageArray writeToFile:datAllString atomically:YES]; 
    } 

    } 

를 교체하려고이 작업을 수행. 그래서 나는 좋은 색인을 찾지 못했다는 것을 알고 있습니다. 어떻게 찾을 수 있습니까?

들으

+0

여기서 무엇을하려고합니까? 어떤 이유로 든 루핑, 복사 및 교체하고 있습니다. 루프 내에서 배열 전체를 파일로 작성하지 않아야합니다. – bryanmac

+0

모든 인덱스에 대해 for 루프가 반복됩니다. "좋은 지표"가 무엇인지 명확하게 설명해 주시겠습니까? – WolfLink

+0

예, 알고 있습니다. Wolflink. 하지만 난 내 배열에 문자열을 찾아 새로운 열쇠로 교체 루프가 필요합니다. – XcodeMania

답변

1
나는 당신이 일을하려고하지만, 여기에 가능한 솔루션이 무엇인지 잘 이해하지 못하는

(당신의 allMessageArray가 배열이 정말 경우 valueForKey를 사용하기 때문에, : 함께!) :

// Browse all messages (you can use "for (NSDictionary *message in allMessageArray)" enumerate loop but because we need the index to replace object, it's the best way to do that) 
for (int index = 0; index < allMessageArray.count; ++index) { 
    // Get current message dictionary 
    NSDictionary *message = [allMessageArray objectAtIndex:index]; 

    // If message came from good sender (you can use isEqualToString: if both objects are NSString instance) 
    if ([[message objectForKey:@"expediteur"] isEqual:[dicoChat2 objectForKey:@"expediteur"]]) { 
    // Create an autoreleased mutable copy of message array to modify some data 
    NSMutableDictionary *messageModified = [NSMutableDictionary dictionaryWithDictionary:message]; 

    // *** Modify what you want in messageModified dictionary *** 

    // Replace original message with modified message in array (assume that allMessageArray is a mutable array) (It's very bad to modify an array in its enumerate loop but because we don't remove/add an object to the array, it's fine to do like that) 
    [allMessageArray replaceObjectAtIndex:index withObject:messageModified]; 

    // *** If you know that you will have always only one message in array with good sender, you can break the loop here *** 
    } 
} 

// Write array to file 
[allMessageArray writeToFile:datAllString atomically:YES]; 
+0

답변을 위해 thx하지만 나는 같은 오류가 있습니다. 우리는 좋은 색인을 가지고 있지 않습니다. – XcodeMania

+0

오케이 지금 무슨 일이 있었는지 이해합니다. 다시 thx – XcodeMania