0

Good Morning,

I am creating my first iphone app so bear with me. I have created a plist and have attached a picture so you can see how I set it up.

I'm trying to add another Item under Clients, which has 3 keyvaluepairs in it.

This is what I have so far but I am not understanding how to edit the saved plist I have.

        NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
    NSDictionary *clientDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistCatPath];
    NSMutableArray *arrayTempClients = [[NSMutableArray alloc] initWithObjects:clientDictionary[@"Clients"], nil];

    NSMutableArray *array1 = [[NSMutableArray alloc] initWithArray:arrayTempClients[0]];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    dict[@"Description"] = @"testAccount";
    dict[@"Username"] = @"customUsername";
    dict[@"Password"] = @"customPassword";
    [array1 addObject:dict];

enter image description here

any help would be greatly appreciated!

ErasmoOliveira
  • 1,416
  • 2
  • 20
  • 40
solarissf
  • 1,199
  • 2
  • 23
  • 58
  • 2
    You can't write on your embedded plist: http://stackoverflow.com/questions/12274530/updating-and-saving-data-in-plist – Larme Mar 09 '15 at 14:15
  • 3
    Just a little observation but I wouldn't save Passwords into your plist I'd suggest looking into keychain service instead for that. – Popeye Mar 09 '15 at 14:17
  • why would I get a negative vote on this question? – solarissf Mar 09 '15 at 14:21
  • am I coming about this the wrong way. I basically load this information on startup, and have the user be able to edit it. What would be the correct way to do this if not a plist? – solarissf Mar 09 '15 at 14:23
  • Question down votes are free, there are a good number here who have many tines more down votes than upvotes. – zaph Mar 09 '15 at 14:23

1 Answers1

1

You can not make any changes to the app and that includes files in it.

If you need a version of the plist you can edit then on first launch copy the plist file to your app's Document directory. Then you can read it into a NSMutableDictionary, add the new data and save it back.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • is there a more practical way that I should be doing this? – solarissf Mar 09 '15 at 14:23
  • Save passwords in the keychain—always, it is secure. For other data create a Data Model and save it with NSArchiver. Note that among other things plists are based on string based keys so there is no type checking. They are generally easily accessed from a connected computer. There is an extra step to know if the item exists, this is a particular problem with BOOLs. – zaph Mar 09 '15 at 14:25
  • is a data model considered core data? since I will only be saving about 9 different words from what I was reading that was overkill. no? – solarissf Mar 09 '15 at 14:28
  • A Data Model is generally just a class and it can use Core Data or more commonly just `@properties`. It can be global with a Singleton or local to a specific operation. It can be as simple as a class with properties declared in the @interface and an empty @implementation. – zaph Mar 09 '15 at 14:30
  • ok I think I'm beginning to get it. So I create class with some properties and use NSArchiver to access it on startup? and try and save the password in keychain – solarissf Mar 09 '15 at 14:38
  • 1
    Yes. For saving with NSArchiver you will have to implement `initWithCoder` and `encodeWithCoder` using the `NSCoder` methods.. – zaph Mar 09 '15 at 14:45