-
归档解档(使用MJExtension,模型,数组)
MJExtension里面已经给我们写好了归档接档的方法,首先你需要在你的model.m里面加上MJCodingImplementation
,这是一个宏定义。然后进行
归档
NSString *filePath = [[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingString:@"归档的文件名(自己取)"];
[NSKeyedArchiver archiveRootObject:模型或者数组 toFile:filePath];
解档
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"归档的文件名(自己取)"];
模型或者数组 = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
使用MJExtension的下面的方法可以说明那些属性不需要归档
+ (NSArray *)mj_ignoredCodingPropertyNames{
return @[@"属性名"];
}
-
保存数组或字典到本地(json数据)
字典或数组转成二进制数据存到本地
#define APP_DOCUMENT [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define DocumentPath(path) [APP_DOCUMENT stringByAppendingPathComponent:path]
NSData *resultData = [NSJSONSerialization dataWithJSONObject:字典或数组 options:NSJSONWritingPrettyPrinted error:nil];
[resultData writeToFile:DocumentPath(@"localData.plist") atomically:YES];
读取本地地址
NSData *data = [NSData dataWithContentsOfFile:DocumentPath(@"localData.plist")];
if (!isEmpty(data)) {
NSDictionary * resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
或
NSArray * resultArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
}
-
沙盒NSUserDefaults
存储
[[NSUserDefaults standardUserDefaults]setObject:@"要保存的数据" forKey:@"要保存的数据的key"];
[[NSUserDefaults standardUserDefaults]synchronize];
读取
[[NSUserDefaults standardUserDefaults]objectForKey:@"要保存的数据的key"];
-
数据库(JKDBModel)
使用方法非常简单,作者的github
介绍的很详细,我就不写了。
github🔗