0

EDIT: this is my full code:

i have a array how look like this:

<?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>

i need to put new datas in this array but in my new data i have this :

<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 is already exist in my array so i have to find the index in my array where sophie appears to put my new data

so i do this to try to find duplicates datas and replace it

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

    }

when i try to use replaceObjectAtindex:i it works but all my datas has been replaced in every index. so i know i didn't find the good index how i can found it?

thx

3
  • What are you trying to do here? You're looping, copy and replacing for what looks like no reason. You also shouldn't be writing the whole array to file inside the loop n times. Commented Mar 6, 2012 at 0:48
  • Your for loop is causing to repeat for every index. Could you clarify what "the good index" is? Commented Mar 6, 2012 at 0:50
  • yes i know it Wolflink. but i need to a loop to find a string in my array and replace it with new key. Commented Mar 6, 2012 at 0:52

1 Answer 1

1

I don't understand very well what you trying to do but here is a possible solution (if your allMessageArray is really an array, because you use valueForKey: with it !!!) :

// 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];
Sign up to request clarification or add additional context in comments.

1 Comment

i thx for answer but i have the same error. we dont have the good index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.