OC-沙箱&归档
来源:互联网 发布:计算机的软件组成 编辑:程序博客网 时间:2024/04/30 07:34
沙箱:
Documents:最长打交道的文件夹,保存长期有用数据 iTunes会备份此文件夹
Library/Caches: 缓存文件夹 装缓存数据 iTunes不会备份
Library/Preferences:偏好设置文件夹 保存用户偏好设置数据 iTunes会备份
tmp:临时文件夹 装临时文件 此文件夹数据会不定时清除 iTunes不会备份
NSLog(@"沙箱的根目录:%@",NSHomeDirectory()); NSString *documentPath1 = [NSHomeDirectory() stringByAppendingString:@"/Documents"]; documentPath1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *documentPath2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]; NSLog(@"%@--%@",documentPath1,documentPath2); //得到临时文件夹 NSString *tmpPath = NSTemporaryDirectory(); NSLog(@"临时文件夹:%@",tmpPath); //得到缓存文件夹 NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject]; NSLog(@"缓存文件夹:%@",cachePath); NSString *filePath = [documentPath1 stringByAppendingPathComponent:@"names.plist"]; NSArray *names = @[@"刘德华",@"张学友",@"郭富城",@"赵四"]; [names writeToFile:filePath atomically:YES];
用户偏好设置Library/Prefrence:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; //保存数据 [ud setInteger:18 forKey:@"age"]; //同步 [ud synchronize]; //获取数据 NSInteger age = [ud integerForKey:@"age"];
归档:把对象转成Data的一种方式
只有实现了NSCoding协议的对象 才能转成NSData
//把UIView 转成Data NSData *viewData = [NSKeyedArchiver archivedDataWithRootObject:self.myView]; [viewData writeToFile:@"/Users/shuan/Desktop/view.arch" atomically:YES];
NSData *data = [NSData dataWithContentsOfFile:@"/Users/shuan/Desktop/view.arch"]; //通过反归档把 nsdata 转回对象 UIView *view = [NSKeyedUnarchiver unarchiveObjectWithData:data];
自定义对象归档:
//自定义对象要采纳NSCoding协议@interface Student : NSObject<NSCoding>@property (nonatomic, copy)NSString *name;@property (nonatomic)NSInteger age;@end
//实现NSCoding协议中的方法#import "Student.h"@implementation Student//实现编码方法 在方法中对每一个属性进行编码- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"];}- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{ self = [super init]; if (self) { self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeIntegerForKey:@"age"]; } return self;}@end
// Student *s = [Student new];// s.name = @"刘德华";// s.age = 28;// // NSData *data = [NSKeyedArchiver archivedDataWithRootObject:s];// // [data writeToFile:@"/Users/tarena/Desktop/student.arch" atomically:YES];// 反归档************ NSData *data = [NSData dataWithContentsOfFile:@"/Users/tarena/Desktop/student.arch"]; Student *s = [NSKeyedUnarchiver unarchiveObjectWithData:data];
0 0
- OC-沙箱&归档
- OC之归档
- OC之自定义归档
- oc基础知识5:归档
- OC-归档:json/plist
- OC对象归档
- OC基础17:归档
- oc 学习(归档)
- OC中的归档和反归档
- OC 归档和解档 总结
- oc中对象归档概念
- OC中的归档解档
- OC学习之XML属性列表归档
- OC学习之NSKeyedArchiver类归档
- OC-本地化操作(序列化,归档)
- oc 对象归档解档<NSCode>
- OC学习篇之---归档和解挡
- OC语言--数据存储(归档&plist&Json)
- 带进度条的按钮
- EventBus源码解析
- 管程机制处理进程同步问题
- 小程序探究
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(55)-Web打印
- OC-沙箱&归档
- STM32的通用定时器的配置
- 约瑟夫问题各种求解办法
- mysql 5.7.14 安装配置方法图文教程
- Spring学习总结一(续2)
- android三级缓存
- 第三次经济大洗牌,你是否抓得住
- 《在雨中等你》读后感
- 文章标题