-3

I'm trying to add multiple objects to a plist, from the same UITextField. I explain better. I have an UITextField from which I add a phone number to a plist. The problem is that I can't add more than one number to that plist from only one textfield. This is my code:

NSMutableDictionary *myDictionary;

NSArray *theArray;

theArray = [[NSArray alloc] initWithObjects:textField.text, nil];

myDictionary = [[NSMutableDictionary alloc] init];

[myDictionary setObject:textField.text forKey:addedTime];


NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PhoneNumbers" ofType:@"plist"];
[myDictionary writeToFile:plistPath atomically:YES];

Is there any way to fix the problem ?

Thank you in advance.

  • 3
    Can you explain a little further what you're trying to do? In particular, how are the phone numbers separated and how do you want them represented in the plist? As it stands, you read a single string value from the text field and store it as a dictionary containing a single text object. – gaige Jun 08 '13 at 17:58
  • 2
    possible duplicate of [Not able to write to Plist](http://stackoverflow.com/questions/5116831/not-able-to-write-to-plist) **and** [of this one too](http://stackoverflow.com/questions/4116179/how-can-i-write-to-a-plist-file) **and** [and of this question as well](http://stackoverflow.com/questions/6193912/strange-problem-with-reading-and-writing-a-plist-file), I can't imagine why it's so hard to search for this... –  Jun 08 '13 at 17:58
  • I can write to the plist, that's not the problem. The problem is that I write what's in the textfield, but seems I can only add one item to the plist. If I add one more, it overwrites the first one. Do you understand ? – user2466833 Jun 08 '13 at 18:06
  • @H2CO3 The specific question is not a dupe. However, OP should read those questions for details on other issues. – bbum Jun 08 '13 at 20:40

1 Answers1

2

writeToFile:atomically: completely replaces whatever is on disk with the contents of the object you to call writeToFile:atomically: on.

If you want to append to whatever is on disk, you'll need to load it first, add to the dictionary, then write the whole thing back.

Note that you should never write anything to [NSBundle mainBundle]. In fact, you can't on a device at all in a production build.

bbum
  • 162,346
  • 23
  • 271
  • 359