我正在尝试以编程方式创建plist文件。这是plist文件的屏幕截图。 http://imgur.com/X8Tg7jS
我找到了这个苹果文档(https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/CreatePropListProgram/CreatePropListProgram.html)然而,我无法弄清楚它是否正确。你如何以编程方式制作这个plist文件?
答案 0 :(得分:1)
此代码生成一个与您链接的结构相同的plist(您必须为for循环中的每个条目提供'title'和'value'的实际值)
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];
答案 1 :(得分:1)
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];
答案 2 :(得分:0)
您只需创建一个字典,其中包含您想要的所有设置作为键/值对。然后我们这个代码,
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];