我想在我的应用程序中创建“UrlPaths.plist”文件以及一个包含 4 或 5 个对象的字典。请帮我创建一个 plist 文件和字典。并从该 plist 文件中读取数据。
我希望 plist 将文件添加到资源文件夹中,并且当时我也想添加字典。我不希望实用地创建 plist,但我希望实用地读取数据。
最佳答案
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;
if ([fileManager fileExistsAtPath: path]) {
data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
}
else {
// If the file doesn’t exist, create an empty dictionary
data = [[NSMutableDictionary alloc] init];
}
//To insert the data into the plist
data[@"value"] = @(5);
[data writeToFile: path atomically:YES];
[data release];
//To retrieve the data from the plist
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
int value1;
value1 = [savedStock[@"value"] intValue];
NSLog(@"%i",value1);
[savedStock release];
关于iphone - 我们如何在 Xcode 项目中创建自己的 plist 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5853735/