1.plist文件的存取
1.1 Document的目录搜索
// 1.拼接字符串
NSString* homePath = NSHomeDirectory(); (获得沙盒路径)
// NSString* docPath = [homePath stringByAppendingString:@"/Documents"];
// NSString* docPath = [homePath stringByAppendingPathComponent:@"Documents"];
// 2.系统提供的搜索
// SearchPath:搜索路径 ForDirectories:哪个文件夹 InDomains:在哪搜索
NSString* docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
1.2// 什么能做plist存储
// 一定要有write to file的方法
如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等类型,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中
// 获取doc目录
// NSDocumentDirectory : 搜索哪个文件夹
// NSUserDomainMask : 在哪搜索
NSString* docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// filePath 存放的文件完整路径名
NSString* filePath = [docPath stringByAppendingPathComponent:@"xx.plist"];
// // 创建一个字典
// NSDictionary* dict = @{ @"key" : @"value",
// @"key1" : @"value1" };
//
// // 存
// [dict writeToFile:filePath atomically:YES];
// 创建array
NSArray* array = @[ @"语句", @"你好" ];
// atomically:线程安全的
[array writeToFile:filePath atomically:YES];
1.3读取数据
// 获取doc目录
NSString* docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// filePath
NSString* filePath = [docPath stringByAppendingPathComponent:@"xx.plist"];
// NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
// NSLog(@"%@", dict);
2.偏好设置的存储
// 存数据
// 1.不需要关心文件名
// 2.快速的做键值对的存储
// 3. NSUserDefaults 实际上就是系统对字典的封装
NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
[ud setObject:@"value" forKey:@"key"];
[ud setBool:YES forKey:@"isOn"];
// 同步 ios8以后不需要调用也可以正常使用
//注意:UserDefaults设置数据时,不是立即写入,而是根据时间戳定时地把缓存中的数据写入本地磁盘。所以调用了set方法之后数据有可能还没有写入磁盘应用程序就终止了。出现以上问题,
[ud synchronize];
// 取数据
NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
NSLog(@"%@", [ud objectForKey:@"key"]);
NSKeyedArchiver 归档和回档
如果对象是NSString、NSDictionary、NSArray、NSData、NSNumber等类型,可以直接用NSKeyedArchiver进行归档和恢复
不是所有的对象都可以直接用这种方法进行归档,只有遵守了NSCoding协议的对象才可以
NSCoding协议有2个方法:
encodeWithCoder:
每次归档对象时,都会调用这个方法。一般在这个方法里面指定如何归档对象中的每个实例变量,可以使用encodeObject:forKey:方法归档实例变量
initWithCoder:
每次从文件中恢复(解码)对象时,都会调用这个方法。一般在这个方法里面指定如何解码文件中的数据为对象的实例变量,可以使用decodeObject:forKey方法解码实例变量
如果父类也遵守了NSCoding协议,请注意:
应该在encodeWithCoder:方法中加上一句
[super encodeWithCode:encode];
确保继承的实例变量也能被编码,即也能被归档
应该在initWithCoder:方法中加上一句
self = [super initWithCoder:decoder];
确保继承的实例变量也能被解码,即也能被恢复
// 存数据
// tmp 文件路径的获取
//将文件存放到tmp临时数据下
NSString* tmpPath = NSTemporaryDirectory();
NSString* filePath = [tmpPath stringByAppendingPathComponent:@"teacher.data"];
Teacher* t = [[Teacher alloc] init];
t.name = @"tom";
t.age = 18;
[NSKeyedArchiver archiveRootObject:t toFile:filePath];
// 取数据
NSString* tmpPath = NSTemporaryDirectory();
NSString* filePath = [tmpPath stringByAppendingPathComponent:@"teacher.data"];
Teacher* t = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%d", t.age);
在Teacher类中
@implementation Teacher
// 告诉系统需要归档哪些属性
- (void)encodeWithCoder:(NSCoder*)aCoder
{
[aCoder encodeInt:_age forKey:@"age111111"];
[aCoder encodeObject:_name forKey:@"name"];
}
// 告诉系统解档哪些属性
- (id)initWithCoder:(NSCoder*)aDecoder
{
if (self = [super init]) {
_age = [aDecoder decodeIntForKey:@"age111111"];
_name = [aDecoder decodeObjectForKey:@"name"];
}
return self;
}
@end