Question

I'm trying to make plist file programmatically. Here is a screenshot of plist file. http://imgur.com/X8Tg7jS

enter image description here

I found this apple document (https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/CreatePropListProgram/CreatePropListProgram.html) However, I couldn't figure out to make it right. How do you make this plist file programmatically?

Was it helpful?

Solution

NSDictionary *dict = @{
    @"data" : @[
        @{ @"title" : @"ababa",
           @"value" : @(56)
        },      
        @{ @"title" : @"AAA",
           @"value" : @(12)
        },      
        @{ @"title" : @"BBB",
           @"value" : @(30)
        },      
        @{ @"title" : @"CCC",
           @"value" : @(10)
        },      
        @{ @"title" : @"DDD",
           @"value" : @(30)
        },      
        @{ @"title" : @"EEE",
           @"value" : @(40)
        }
    ]
};

[dict writeToFile:filename
       atomically:YES];

OTHER TIPS

This code produces a plist with the same structure as the one you linked to (you must provide the actual values for 'title' and 'value' for each entry inside the for loop)

NSMutableDictionary* dict = [NSMutableDictionary new];

NSMutableArray* data = [NSMutableArray new];

for(NSUInteger i=0; i < someMaxValue; i++){

    NSDictionary* item = @{
                          @"title": someTitleString,    // Get actual value from somewhere
                          @"value": @(someTitleNumber)  // (depends on your code)
                         };

    [data addObject:item];
}

[dict setObject:data forKey:"data"];

BOOL writeResult = [dict writeToFile:filePath atomically:YES];

You simply can create a dictionary with all the settings you want as key/value pairs. And then us this code,

 NSDictionary *item = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"x", @"a",
                          @"y", @"b",
                          nil];

    NSArray *items = [NSArray arrayWithObjects: item, nil];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"fileName.plist"];

    // Write array of items to filesystem
    [items writeToFile:path atomically:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top