Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pashik

macrumors member
Original poster
Jul 16, 2008
43
0
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>
 
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.

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

Thanks for help
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.