以这种方式获得的Plist文件没有关于xcode视图的数据,但实际上它已经被编写,你可以通过代码找到数据
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *plistPath = [paths objectAtIndex:0];
NSMutableArray *marr = [NSMutableArray array];
NSString *filename=[plistPath stringByAppendingPathComponent:@"city.plist"];
NSLog(@"filename == %@",filename);
[marr addObject:@"字符串"];
[marr writeToFile:filename atomically:YES];
答案 0 :(得分:1)
如果聚合中的所有对象都派生自NSDictionary,NSArray,NSString,NSDate,NSData或NSNumber类,则可以在Objective-C中创建属性列表。
使用以下代码:
//Get the documents directory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"plist.plist"] ];
}
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 setObject:@"iPhone 6 Plus" forKey:@"value"];
[data writeToFile:path atomically:YES];
//To retrieve the data from the plist
NSMutableDictionary *savedValue = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
NSString *value = [savedValue objectForKey:@"value"];
NSLog(@"%@",value);
有关详情,请点击here
Apple还提供了一个用于创建plist文件here的演示项目。
答案 1 :(得分:1)
如果您要以编程方式创建Plist,请按照以下步骤操作:
这会添加到您的项目中。
答案 2 :(得分:0)
我编写了一个缓存API响应的类,所以稍后当你请求Countries端点时,它会检查已定义的端点以进行缓存,如果它在列表中找到它并且它存在于缓存中,它将返回缓存的端点并且不会完成执行HTTP请求,在文档中我提供了如何实现它的步骤。
答案 3 :(得分:0)
以下函数将可serilizable对象(字典,数组)作为输入并将其转换为plist并将其写入应用程序的文档中:
+ (BOOL) writeBundelPlist : (NSString *) plistName with : (id) newPlistData {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:plistName];
if (![fileManager fileExistsAtPath:plistPath]) {
NSString *bundle = [[NSBundle mainBundle] pathForResource:[plistName stringByDeletingPathExtension] ofType:[plistName pathExtension]];
NSLog(@"plistPath = %@", plistPath);
[fileManager copyItemAtPath:bundle toPath:plistPath error:&error];
}
return [newPlistData writeToFile:plistPath atomically:YES];
}
答案 4 :(得分:0)
这种方法非常有用,感谢evervone,感谢rmaddy
做{ let data = try NSPropertyListSerialization.dataWithPropertyList(response.result.value as!NSMutableDictionary,format:NSPropertyListFormat.XMLFormat_v1_0,options:0)
data.writeToFile(countryListPath, atomically: true)
}catch{
}