Search code examples
objective-cplist

How can i update plist file which has many group item in Objective-C?


screenshot

I have a plist file like this, and now i want to edit the row.

For example, i want to edit row "ZFILMSEAT" in Item 1.

Please somebody suggest me how to update data in plist file.


Solution

  • First of all, When you retrieve the .plist file, store it as NSMutableDictionary.

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"your plist name" ofType:@"plist"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    NSMutableArray *filmsPlaying = [[NSMutableArray alloc] iniWithArray:[dict objectForKey:@"FilmsPlaying"]];
    NSMutableDictionary *filmToEdit = [filmsPlaying objectAtIndex:1];// IRL run a loop to get your desired film
    NSString *newSeats = @"1-2-9";
    [filmToEdit setObject:newSeats forKey:@"ZFILMSEAT"];
    [filmsPlaying replaceObjectAtIndex:1 withObject:filmsToEdit];
    [dict setObject:filmsPlaying forKey:@"FilmsPlaying"];
    

    Here you have the film you want to edit. After you have made your edition to that dictionary, you need to write it back to App's document dir. (NOTE: You CANNOT save it back to your main bundle but you can write it to Documents instead).

    NSString *pathForPlist = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    pathForPlist = [savingPath stringByAppendingPathComponent:@"your plist name.plist"];
    [dict writeToFile:savingPath automically:YES];
    

    To edit it in the main bundle without any programming, you can simply open it in any text editor or xcode itself and make your edition. Nothing techie about that.