我试图在swift中将数据保存到plist文件,但是数据没有显示,因为它在读取plist时保存了.这是我正在使用的代码.
var documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
var path : NSString = documentsDirectory.stringByAppendingPathComponent("data.plist")
var data : NSMutableDictionary = NSMutableDictionary(contentsOfFile: path)
data.setObject(self.object, forKey: "key")
data.writeToFile(path, atomically: true)
Run Code Online (Sandbox Code Playgroud)
编辑:我听说最好的方法是写入文档目录,所以我的问题是如何写入该目录中的文件?
我正在尝试将一个NSDictionary写入plist,但是当我打开plist时,没有数据写入它.从日志中我的路径看起来正确,我的代码非常标准.有任何想法吗?
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
for (id key in dictionary) {
NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"FormData" ofType:@"plist"];
NSLog(@"path:%@", path);
[dictionary writeToFile:path atomically:YES];
Run Code Online (Sandbox Code Playgroud)