Adding new items to plist programmatically

1.1k views Asked by At

In my last question I asked for advice on saving/loading from file. Plist advice was pretty awesome, but of course I had to run into some kind of problem. I already populated my TableView>DetailView controllers with plist. Now I have to somehow save my data(from parse.com). Here is my current plist, I filled it manually and for sake of simplicity(please don't kill me) I made all items strings:

<plist version="1.0">
<dict>
    <key>orderID</key>
    <array>
        <string>idKMVX956</string>
        <string>idEWUQ321</string>
    </array>
    <key>orderdate</key>
    <array>
        <string>28.06.2015</string>
        <string>26.06.2015</string>
    </array>
    <key>match</key>
    <array>
        <string>Team 3 vs Team 4</string>
        <string>Team 1 vs Team 2</string>
    </array>
    <key>date</key>
    <array>
        <string>20.06.2015</string>
        <string>20.06.2015</string>
    </array>
    <key>sector</key>
    <array>
        <string>2</string>
        <string>1</string>
    </array>
    <key>seat</key>
    <array>
        <string>R2S75</string>
        <string>R1S1</string>
    </array>
    <key>price</key>
    <array>
        <string>120</string>
        <string>60</string>
    </array>
</dict>
</plist>

Now my question is - how to add new order to this plist. Just for sake of testing I wrote this method:

- (void)saveToPlist
{
    NSString *matchname = @"Team 5 vs Team 6";
    NSString *orderHistoryFile = [[NSBundle mainBundle] pathForResource:@"OrderHistory" ofType:@"plist"];
    orders = [[NSMutableDictionary alloc] initWithContentsOfFile:orderHistoryFile];
    match = [orders objectForKey:@"match"];
    NSLog(@"matches: %@", match);
    [match addObject:matchname];
    NSLog(@"addedTeam; %@", match);
    [match writeToFile:orderHistoryFile atomically:YES];
}

But while second NSLog shows that indeed line "Team 5 vs Team 6" was added to array - no changes appear in plist file and do not appear on tableview. Why?

@Update alright I see where my mistake was with writetofile. However now after executing this method - it works once and I can see details. Then I can't load data anymore and table does not populate. Why? Update2 So i can't write in my bundle directory(ty for advice). That made me try to move whole process to documents. Here is some code i wrote:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *filePath = [[self fileDirectory] stringByAppendingString:@"OrderHistory.plist"];
    if (![[NSFileManager defaultManager]fileExistsAtPath:filePath]){
     [[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"OrderHistory" ofType:@"plist"] toPath:filePath error:nil]; 
     }
    orders = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    orderId = [orders objectForKey:@"orderID"];
    NSLog(@"count orders: %d", [orderId count]);
}
- (NSString *)fileDirectory{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

}

Now my NSLog counting items outputs 0. Everything works fine with bundle directory, so I suspect that file is not even created in documents. Where is my mistake?

2

There are 2 answers

3
Droppy On

You cannot write into the app bundle, so you will need to save the file into the Documents folder, or equivalent (like Caches folder).

0
0yeoj On

Your mistake is in the line: [[self fileDirectory] stringByAppendingString:@"OrderHistory.plist"];

It should be -stringByAppendingPathComponent, when you append it like string you'll miss the / which is relevant for directoryPath.