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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-31-2010, 07:46 AM   #1 (permalink)
WJK
Registered Member
 
Join Date: Dec 2010
Posts: 3
WJK is on a distinguished road
Default Writing an array to a dictionary in a Plist

I have a Plist with the following structure:

Dictionary
Array1
Dictionary
String
String
String
Dictionary
Dictionary
Dictionary
Dictionary
Array2
Dictionary
Dictionary
Dictionary
Dictionary
Dictionary
Dictionary

Arrays 1 and 2 correspond to two sections of a grouped table. I have no problem transfering the strings in the dictionaries as needed into the cells of the table. Now I'm working on adding code to edit the strings. I hit a bit of a wall trying to write data back to the main dictionary. I start off by creating a new dictionary with the edited strings:

NSDictionary *editedData = [NSDictionary dictionaryWithObject:dataOneTextField.text forKey:@"dataOne"];

editedData = [NSDictionary dictionaryWithObject:dataTwoTextField.text forKey:@"dataTwo"];

editedData = [NSDictionary dictionaryWithObject:dataThreeTextField.text forKey:@"dataThree"];

Then I extract the array that contained the edited dictionary and replace the edited dictionary:

int tSection = [section intValue];
NSString *sectionsInTable = [keys objectAtIndex:tSection];
NSMutableArray *dictionariesInSection = [stations objectForKey:sectionsInTable];
int tRow = [rowToEdit intValue];
[dictionariesInSection replaceObjectAtIndex:tRow withObject:editedData];


The problem is I don't know the syntax or method to write the array back in the main dictionary and can't Google any suitable examples.

So two questions. Is what I've done so far correct and if not what is. And, how do you write the array back to the main dictionary.


Thanks for your help
WJK is offline   Reply With Quote
Old 12-31-2010, 07:57 AM   #2 (permalink)
Registered Member
 
Join Date: Oct 2010
Location: Germany
Posts: 11
LinusR is on a distinguished road
Default

show the contents of the property list in the code brackets. Then it is easier to understand the structure of your plist.
LinusR is offline   Reply With Quote
Old 12-31-2010, 01:38 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by WJK View Post
I have a Plist with the following structure:

Dictionary
Array1
Dictionary
String
String
String
Dictionary
Dictionary
Dictionary
Dictionary
Array2
Dictionary
Dictionary
Dictionary
Dictionary
Dictionary
Dictionary

Arrays 1 and 2 correspond to two sections of a grouped table. I have no problem transfering the strings in the dictionaries as needed into the cells of the table. Now I'm working on adding code to edit the strings. I hit a bit of a wall trying to write data back to the main dictionary. I start off by creating a new dictionary with the edited strings:

NSDictionary *editedData = [NSDictionary dictionaryWithObject:dataOneTextField.text forKey:@"dataOne"];

editedData = [NSDictionary dictionaryWithObject:dataTwoTextField.text forKey:@"dataTwo"];

editedData = [NSDictionary dictionaryWithObject:dataThreeTextField.text forKey:@"dataThree"];

Then I extract the array that contained the edited dictionary and replace the edited dictionary:

int tSection = [section intValue];
NSString *sectionsInTable = [keys objectAtIndex:tSection];
NSMutableArray *dictionariesInSection = [stations objectForKey:sectionsInTable];
int tRow = [rowToEdit intValue];
[dictionariesInSection replaceObjectAtIndex:tRow withObject:editedData];


The problem is I don't know the syntax or method to write the array back in the main dictionary and can't Google any suitable examples.

So two questions. Is what I've done so far correct and if not what is. And, how do you write the array back to the main dictionary.


Thanks for your help

Several things.

First:

When you read a plist, all the dictionaries and arrays you get are immutable, even if you created the plist using mutable dictionaries and/or arrays. Thus, you can't replace an of the contents of those dictionaries or arrays, or add to them. If you want to change the contents of the dictionaries and arrays you read from your plist, you need to convert the dictionaries and arrays to their mutable forms. Both objects support the method mutableCopy, so you could write a routine that recursively walked your dictionary/array structure and replaced all the dictionaries and arrays with their mutable counterparts.

That's the answer to your problem: Make the dictionaries and arrays in your tree mutable. Then you can replace any element you want.

To replace an entry in a mutable dictionary, you just use the method setObject:forKey: For mutable arrays, the method to replace an object is replaceObjectAtIndex:withObject:

Next issue:

This code doesn't make any sense:

Code:
//line 1
NSDictionary *editedData = [NSDictionary dictionaryWithObject: dataOneTextField.text forKey:@"dataOne"];

//line 2
editedData = [NSDictionary dictionaryWithObject: dataTwoTextField.text forKey:@"dataTwo"];

//line 3
editedData = [NSDictionary dictionaryWithObject: dataThreeTextField.text forKey:@"dataThree"];
In line 1 you create a dictionary in editedData with a value from dataOneTextField.text and a key of @"dataOne"

In line 2, you create a new dictionary with a value from dataTwoTextField.text and a key of @"dataTwo", and save that new dictionary into editedData, thus orphaning the object you created in line 1.iPhone Dev SDK Forum - Threads Tagged with iphone

In line 3, you create a third dictionary with a different value and key, and again orphan the dictionary you created in line 2. After those 3 lines, editedData will contain your third dictionary, with only a single key/value pair.

You want to use code like this instead:

Code:
NSMutableDictonary editedData = [NSMutableDictionary dictionaryWithCapacity: 3];
[editedData setObject: dataOneTextField.text forKey:@"dataOne"];
[editedData setObject: dataTwoTextField.text forKey:@"dataTwo"];
[editedData setObject: dataThreeTextField.text forKey:@"dataThree"];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
array, dictionary, plist, writeobject

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: 363
8 members and 355 guests
blueorb, fredidf, iAppDeveloper, iGamesDev, MarkC, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

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