How to update NSMUtableArray with NSMutablDictinoary

Discussion in 'iOS Programming' started by pashik, May 8, 2010.

  1. pashik macrumors member

    Joined:
    Jul 16, 2008
    #1
    Hello.

    I have plist with arrays of dictionaries.

    Removing of object looks easy enough:
    Code:
    NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 	
    NSMutableArray	sectionList = [NSMutableArray arrayWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",[filePaths objectAtIndex: 0], @"myplist.plist"]];
    
    NSMutableDictionary *sectionItems = [sectionList objectAtIndex:0];
    NSMutableArray *items = [sectionItems objectForKey:@"items"];
    [items removeObjectAtIndex:0];
    But problem i met is how to update "sectionList" after object was removed from "items" or after new object was added into "items" so updated data can be saved into plist file.

    Thank you for help.

    Here is plist example:
    Code:
    <plist version="1.0">
    <array>
    	<dict>
    		<key>items</key>
    		<array>
    			<dict>
    				<key>id</key>
    				<string>1</string>
    				<key>name</key>
    				<string>item title 1</string>
    			</dict>
    			<dict>
    				<key>id</key>
    				<string>13</string>
    				<key>name</key>
    				<string>item title 2</string>
    			</dict>
    		</array>
    		<key>sectionName</key>
    		<string>Section A</string>
    	</dict>
    	<dict>
    		<key>items</key>
    		<array>
    			<dict>
    				<key>id</key>
    				<string>1</string>
    				<key>name</key>
    				<string>item title 3</string>
    			</dict>
    			<dict>
    				<key>id</key>
    				<string>13</string>
    				<key>name</key>
    				<string>item title 4</string>
    			</dict>
    			<dict>
    				<key>id</key>
    				<string>4</string>
    				<key>name</key>
    				<string>item title 5</string>
    			</dict>
    			<dict>
    				<key>id</key>
    				<string>444</string>
    				<key>name</key>
    				<string>item title 6</string>
    			</dict>
    		</array>
    		<key>sectionName</key>
    		<string>Section B</string>
    	</dict>
    	<dict>
    		<key>items</key>
    		<array>
    			<dict>
    				<key>id</key>
    				<string>1</string>
    				<key>name</key>
    				<string>item title 7</string>
    			</dict>
    			<dict>
    				<key>id</key>
    				<string>13</string>
    				<key>name</key>
    				<string>item title 8</string>
    			</dict>
    		</array>
    		<key>sectionName</key>
    		<string>Section C</string>
    	</dict>
    </array>
    </plist>
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    The array doesn't need updating since you're modifying objects that are within it. If you want to save it to file when you modify the array or any object within the array, use writeToFile:atomically:
     
  3. pashik thread starter macrumors member

    Joined:
    Jul 16, 2008
    #3
    Thanks for help
     

Share This Page