iOS的数据存储之plist,你用对了么
2018-12-06 本文已影响0人
swift_honor
MacBook-Pro-Desk-with-Cookies.jpg
iOS的存储方式很多,今天我们来看看plist的简单存储与读取
存储和修改plist文件
+ (void)saveKeywords:(NSArray *)array {
if (!IS_VALID_ARRAY(array)) {
return;
}
NSString *path = [TYTools getDocumentsPath];
path = [path stringByAppendingPathComponent:@"TYKeywords.plist"];
BOOL succ = [array writeToFile:path atomically:YES];
if (succ) {
NSLog(@"存储成功");
} else {
NSLog(@"存储失败");
}
}
获取plist文件数据
+ (NSArray *)getKeyWordsFromPlist {
NSString *path = [TYTools getDocumentsPath];
path = [path stringByAppendingPathComponent:@"TYKeywords.plist"];
return [NSArray arrayWithContentsOfFile:path];
}
删除plist文件,即删除数据
+ (void)removeAllKeywords {
NSString *path = [TYTools getDocumentsPath];
path = [path stringByAppendingPathComponent:@"TYKeywords.plist"];
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
if (error == nil) {
NSLog(@"删除成功");
}
}
附加方法 - 获取Documents路径
+ (NSString *)getDocumentsPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}