5种本地存储方式
- NSUserDefaults(Preference偏好设置)
- plist存储
- 归档
- SQLite3(后续补充)
- CoreData(后续补充)
沙盒介绍
- Documents:用于存储用户数据,可被iTunes备份
- Library:包含两个子目录
可创建子文件夹。可以用来放置您希望被备份但不希望被用户看到的数据。该路径下的文件夹,除Caches以外,都会被iTunes备份。- Caches:适合存储体积大,不需要备份的非重要数据
- Preferences:通常保存应用的设置信息(NSUserDefaults)
- tmp:用于存放临时文件,不会被iTunes备份
获取文件目录的路径
//document文件夹路径
#define PATH_AT_Document(name) [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]
//temp文件夹路径
#define PATH_AT_Tmp(name) [NSTemporaryDirectory() stringByAppendingPathComponent:name]
//cache文件夹路径
#define PATH_AT_Cache(name) [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]
//Libary文件夹路径
#define PATH_AT_Library(name) [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]
1. NSUserDefaults
- NSUserDefaults 官方文档
2. plist存储
特点:只能存储OC常用数据类型(NSString、NSDictionary、NSArray、NSData、NSNumber等类型)而不能直接存储自定义模型对象。
2.1 plist文件的创建与删除
NSFileManager *manager = [NSFileManager defaultManager];
// 获取文件路径 Documents
NSString *plistPath = PATH_AT_Document(@"tst.plist");
// 开始创建文件
[manager createFileAtPath:plistPath contents:nil attributes:nil];
// 移除文件
//[manager removeItemAtPath:plistPath error:nil];
2.2 plist 存储/读取字典数据
// 存储
NSDictionary *writeDic = @{
@"a": @"aaa",
@"b": @"bbb",
@"c": @"ccc",
@"d": @"ddd"
};
[writeDic writeToFile:plistPath atomically:YES];
// 读取
NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(@"plist - 读取字典数据:%@", readDic);
2.3 plist 存储/读取数组数据
NSArray *writeArray = @[@"a", @"b", @"c", @"d", @"e"];
// 存储
[writeArray writeToFile:plistPath atomically:YES];
// 读取
NSArray *readArray = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"plist - 读取数组数据: %@", readArray);
3. 归档
3.1. 简单归档:指把简单的字符串、数组等对象进行归档
// 初始化需要归档的数组
NSArray *array = @[@"a", @"b", @"c"];
// 创建归档文件存储路径
NSString *path = PATH_AT_Tmp(@"file.archive"); // 文件后缀随意
// 归档
[NSKeyedArchiver archiveRootObject:array toFile:path];
// 解归档
NSArray *tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// 输出打印
NSLog(@"kkk --- %@", tempArray);
3.2. data归档:先新建一个可变的data对象,然后通过创建一个NSKeyedArchiver实例对象对各种类型的数据进行归档到data的归档文件中
// 创建归档文件存储路径
NSString *path = PATH_AT_Tmp(@"file.archive");
// 创建可变data
NSMutableData *data = [NSMutableData data];
// 创建归档实例
NSKeyedArchiver *keyArch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
// 归档
[keyArch encodeObject:@"勇往直前" forKey:@"name"];
[keyArch encodeInteger:18 forKey:@"age"];
[keyArch finishEncoding];
[data writeToFile:path atomically:YES];
// 解归档
NSData *outData = [NSData dataWithContentsOfFile:path];
NSKeyedUnarchiver *keyUnarch = [[NSKeyedUnarchiver alloc] initForReadingWithData:outData];
NSString *name = [keyUnarch decodeObjectForKey:@"name"];
NSInteger age = [keyUnarch decodeIntegerForKey:@"age"];
NSLog(@"name: %@, age: %ld", name, (long)age);
3.3. 自定义归档
定义一个类UserInfoModel,需遵守NSCoding协议
- UserInfoModel.h文件中定义name、age和sex三个属性
#import
NS_ASSUME_NONNULL_BEGIN
@interface UserInfoModel : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) NSString *sex;
@end
NS_ASSUME_NONNULL_END
- UserInfoModel.m文件需要重写一个归档方法、一个解归档方法
#import "UserInfoModel.h"
@implementation UserInfoModel
// 重写归档方法,把调用者的属性进行归档
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.name forKey:@"name"];
[coder encodeInteger:self.age forKey:@"age"];
[coder encodeObject:self.sex forKey:@"sex"];
}
// 重写解归档方法,把属性赋值给新对象的对应属性
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super init];
if (self) {
self.name = [coder decodeObjectForKey:@"name"];
self.age = [coder decodeIntegerForKey:@"age"];
self.sex = [coder decodeObjectForKey:@"sex"];
}
return self;
}
@end
- 归档与解归档的应用
// 初始化对象
UserInfoModel *model = [[UserInfoModel alloc] init];
model.name = @"遇见";
model.age = 18;
model.sex = @"Man";
// 创建归档文件存储路径
NSString *path = PATH_AT_Tmp(@"file.archive");
// 归档
[NSKeyedArchiver archiveRootObject:model toFile:path];
// 解归档
UserInfoModel *outModel = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
// 输出
NSLog(@"name: %@, age: %ld, sex: %@", outModel.name, (long)outModel.age, outModel.sex);