replaceObjectAtIndex nsmutableArray

时间:2012-03-06 00:41:52

标签: iphone xcode 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>

sophie已经存在于我的数组中所以我必须在我的数组中找到索引,其中sophie似乎放了我的新数据

所以我这样做是为了试图找到重复的数据并替换它

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];
     }

    }

当我尝试使用replaceObjectAtindex:我工作但我的所有数据都已在每个索引中被替换。 所以我知道我没有找到好的指数 我怎么能找到它?

THX

1 个答案:

答案 0 :(得分: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];