Breaking News
Home / iOS / iOS Snippets / How to save an NSArray into Plist with a Key?

How to save an NSArray into Plist with a Key?

Saving an NSArray with a Key into PList is easy. You can start with a NSMutableDictionary and add each array as an entry with a key. Finally save into file.

+(void) saveArrayIntoPlist:(NSArray*)aArray withKey:(NSString*)arrayKey {
   
    CCLOG(@”savePhotoIdArrayToDoc”);
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [NSStringstringWithFormat:@”%@/info.plist”, docDir];
   
    NSMutableDictionary *info = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
   
    if (info != nil) {    
        [info setObject:aArray forKey:arrayKey];
        [info writeToFile:filePath atomically:YES];
        [info release];
    }
    else {
        info = [NSDictionarydictionaryWithObject:aArray forKey:arrayKey];
        [info writeToFile:filePath atomically:YES];
    }<!>
   

[pb_builder]

Check Also

EKCalendar – That account does not allow calendars to be added or removed

Usecase When I am trying to create a new Calendar programmatically using the EventKit API …

Advertisment ad adsense adlogger