Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-16-2010, 08:31 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Location: Germany
Posts: 11
LinusR is on a distinguished road
Question Writing

Hi all,

I have a problem: I have a property list looking like this:
Code:
<dict>
	<key>fruits</key>
	<dict>
		<key>fruit1</key>
		<string>Banana</string>
		<key>fruit2</key>
		<string>Apple</string>
	</dict>
</dict>
I would like to change the value of fruit 1. How can I do this? My actual code is:
Code:
NSString *fileDirectory;
	fileDirectory = [[[NSBundle mainBundle] pathForResource:@"Fruits" ofType:@"plist"] retain];
	NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fileDirectory];
I don't know what to do now. My actual not working code is:
Code:
	[plistDict setValue:fruit1 forKey:@"Peach"];
	[plistDict writeToFile:fileDirectory atomically: YES];
Now a new key and value gets build.

Can anyone help?

Thanks for every help

Last edited by LinusR; 12-16-2010 at 08:34 AM.
LinusR is offline   Reply With Quote
Old 12-20-2010, 05:36 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 159
MiniRobinho is on a distinguished road
Default

You've accessed the plist fine, but you are setting the value wrong.

First get the dictionary with key "fruits":
Code:
NSMutableDictionary *fruits = [plistDict objectForKey:@"fruits"];
In fruits set the value "Peach" for the key "fruit1":
Code:
[fruits setValue:@"Peach" forKey:@"fruit1"];
[plistDict writeToFile:fileDirectory atomically: YES];
Hope that makes sense. If you don't know why certain things have been done just say
MiniRobinho is offline   Reply With Quote
Old 12-23-2010, 05:22 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Location: Germany
Posts: 11
LinusR is on a distinguished road
Default

Thanks for help, but I think I do something wrong.
Now I have code like this:
Code:
NSString *fileDirectory;
	fileDirectory = [[[NSBundle mainBundle] pathForResource:@"Datas" ofType:@"plist"] retain];
	NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fileDirectory];
	
	NSMutableDictionary *plistDictName = [plistDict objectForKey:@"Fruit"];
	
	[plistDictName setValue:@"Apple" forKey:@"fruit1"];
But it doesn't work. Then I created a new dict in the Plist file "Datas.plist" with the name "Fruit" but that does not work as well. My "Datas.plist" file looks like this now:

Code:
<dict>
	<key>Fruit</key>
	<dict>
		<key>fruit1</key>
		<string></string>
	</dict>
</dict>
Nothing happens when I click the button in my application. The string of the key "fruit1" keeps empty.

What do I wrong?
LinusR is offline   Reply With Quote
Old 12-23-2010, 11:25 AM   #4 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 159
MiniRobinho is on a distinguished road
Default

Try the below - setObject instead of setValue.

Code:
[plistDictName setObject:@"Apple" forKey:@"fruit1"];
Make sure that you are saving at the end as well:

Code:
[plistDict writeToFile:fileDirectory atomically: YES];
MiniRobinho is offline   Reply With Quote
Old 12-24-2010, 02:50 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2010
Location: Germany
Posts: 11
LinusR is on a distinguished road
Default

thanks for help, now it works. Thanks!
LinusR is offline   Reply With Quote
Reply

Bookmarks

Tags
plist, property, property list

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 462
18 members and 444 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, ilmman, j.b.rajesh@gmail.com, jeroenkeij, mer10, mkjarred, Music Man, myach, pipposanta, QuantumDoja, robsmy, sacha1996, SLIC, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:32 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0