Quote:
Originally Posted by WJK
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"];