【问题标题】:How to save NSString under Key in info plist?如何在信息 plist 中的 Key 下保存 NSString?
【发布时间】:2016-06-18 08:55:32
【问题描述】:
我想将 NSString 保存在 info plist 中的 Background image 键下。我怎样才能做到这一点 。有什么帮助吗?
我不知道如何在 info.plist 中读取/写入数据。谁能告诉我该怎么做?
【问题讨论】:
标签:
objective-c
swift
nsstring
save
info.plist
【解决方案1】:
更新或插入数据。
NSError *错误;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Mysoot.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Myspot" ofType:@"plist"]; //5
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
[数据 removeObjectForKey:@"BackgroundImage"];
[data setObject:imageName forKey:@"BackgroundImage"];
[data writeToFile: path atomically:YES];
【解决方案2】:
按照以下步骤操作:
右键单击您应用的 info.plist > 打开为 > 源代码