IOS巅峰之归档与反归档

来源:互联网 发布:信息系统网络架构图 编辑:程序博客网 时间:2024/04/30 17:15
1.沙盒
- (void)path{ 
// 咱们每运行一次, 相当于重新安装
// 重新安装, 就重新分配一个沙盒, 所以你每次运行路径都不一样
// NSDocumentationDirectory  要打印文件夹地址
// NSUserDomainMask  搜索范围
// 该文件夹, 一般存储用户的一些数据
NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
// 找到相对路径
NSString *document = [documentsPathArr lastObject];

// 缓存文件夹路径
// 该文件, 一般存储缓存文件
NSArray *cachesPathArr = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesPath = cachesPathArr[0];

// 打印temp文件夹(临时文件)
// 该文件一般存储临时文件
NSString *tempPath = NSTemporaryDirectory();
// 打印沙盒主目录路径
NSString *homePath = NSHomeDirection();
}

// 简单对象写入文件
// 注意:如果你写入字典或者数组, 那么数组字典中存储数据, 必须是简单对象, 无法写入复杂对象
- (void)writeFile
{
// 简单对象:字符串, 字典, 数组, data….系统写好的类
// 写入文件的路径
// 在documents 路径下写入, xiaoshou.txt
NSArray *documents = NSSearchPathForDiectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = documents[0];
// 拼接要写入的文件路径
NSString *path = [documentsPath stringByAppendingPathComponent:@“/xiaoshou.txt”];
NSString *str = @“呵呵”;
// atomically 如果YES在你写入的过程中, 出现程序崩溃不影响写入
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

// 简单对象写入步骤
// 1.拼接要写入的路径(注意, 路径一定要拼对);
//  2.调用写入方法

// 写入一个数组, 文件名shuzu.txt
// 必须给后缀类型, 你不给, 就默认是txt格式
NSString *arrPath = [documentsPath stringByAppendingPathComponent:@“/shuzu.plist”];
NSArray *array = @[@“你好”, @“我很好”];
//调用写入方法
[array writeToFile:arrPath atomically:YES];

// 写入一个字典, 文件名dic.txt
NSString *dicPath = [documentsPath stringByAppendingPathComponent:@“/dic.txt”];
NSDictionary *dic = @{@“a”, @“xxx”};
[dic writeToFile:dicPath atomically:YES];

// data的写入, 后最.da
NSString *dataPath = [documentsPath stringByAppendingPathComponent:@“/data.da”];
NSString *dataStr = @“你好”;
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
// 写入文件
[data writeToFile:dataPath atomically:YES];
}

// 简单对象读取写入的文件
- (void)readingFile
{
// 读字符串
// 获取路径
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = documents[0];
// 拼接要写入的文件路径
NSString *path = [documentsPath stringByAppendingPathComponent:@“/xiaoshuo.txt”];
// 从路径当中读取字符串
NSString *str = [[NSArray alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

// 读数组
NSString *arrPath = [documentsPath stringByAppendingPathComponent:@“/shuzu.plist”];
NSArray *array = [NSArray arrayWithContentsOfFile:arrPath];

// 读取字典
NSString *dicPath = [documentsPath stringByAppendingPathComponent:@“dic.txt”];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];

// 读取data
// 先读出data
NSString *dataPath = [documentsPath stringByAppendingPathComponent:@“data.da”];
NSData *data = [NSData dataWithContentsOfFile:dataPath];
// 将data转化成字符串
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


// 复杂对象, 进行持久化
// 需要遵守一个协议<NSCoding>
// 声明一个model
属性.h
@property (nonatomic, retain)NSString *name;
@property (nonatomic, assign)NSInteger age;
@property (nonatomic, retain)NSData *data;

.m
// 对复杂对象进行持久化, 叫做归档与反归档(编码与解码)
// 归档方法, 编码成可以持久化的格式
- (void)encodeWithCoder:(NSCoder *)aCoder
{
// 对每个属性, 都要进行重新编码
// 注意:属性的类型
[aCoder encodeObject:self.name forKey:@“name”];
// 除了对象类型, 其他类型, 都有特殊的编码类型
[aCoder encodeInteger:self.age forKey:@“age”];
[aCoder encodeObject:self.data forKey:@“data”];
}
// 反归档方法, 解码的过程
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
// 解码的过程
// 跟编码一样, 除了对象类型以外, 也是有特殊解码方法
// 注意:编码的时候, 你给的key要和解码key一样
self.name = [aDecoder decodeObjectForKey:@“name”];
self.age = [aDecoder decodeIntegerForKey:@“age”];
self.data = [aDecoder decodeObjectForKey:@“data”];
}
return self;
}

// 创建文件夹
- (void)createFile
{
// 在documents下创建一个Download文件夹
NSArray *documentsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString  *documentPath = documentsArr[0];
// 拼接路径
NSString *downloadPath = [documentPath stringByAppendingPathComponent:@“Download”];
// 创建文件夹
// 文件管理者, 这个类是个单例类, 用来对文件进行操作
NSFileManager *manager  = [NSFileManager defaultManager];
// withIntermediateDirectories 如果你填YES情况下, 要创建的文件存在的话, 可以对其覆盖, 反之, 文件存在的话, 不能对其覆盖(创建失败)
BOOL isCreateFile = [manager createDirectoryAtPath:downloadPath withIntermediateDirectories:YES attributes:nil error:nil];
}

#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define kCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMainMask, YES) lastObject]

// 复制文件夹
- (void)copyFild
{
// library 下的Caches文件夹download复制到doucment文件
NSString *oldPath = [kCachesPath stringByAppendingPathComponent:@“Download”];
NSString *newPath = [kDocumentPath stringByAppendingPathComponent:@“Download”];
// 创建文件管理对象
NSFileManager *manager = [NSFileManager defaultManager];
// 复制
BOOL isCopy = [manager copyItemAtPath:oldPath toPath:newPath error:nil];
}

// 删除文件夹
- (void)deleteFild
{
// 获取要删除的路径
NSString *deletePath = [kDocumentPath stringByAppendingPathComponent:@“Download”];
// 创建文件管理对象
NSFileManager *manage = [NSFileManager defaultManager];
// 删除
BOOL isDelete [manage removeItemAtPath:deletePath error:nil];
}


// 判断一个文件夹是否存在(经常使用)
- (void)isExistFild
{
// 获取要判断路径
NSString *path = [kCachesPath stringByAppendingPathComponent:@“Download”];
// 创建文件管理对象
NSFileManager *manager = [NSFileManager defaultManager];
BOOL isExist = [manager isExecutableFileAtPath:path];
}


// 复杂对象归档
- (void)archiver
{
// 初始化对象
JJModel *model = [[JJModel alloc] init];
// 赋值对象
model.name = @“mJJ”;
model.age = 23;
// 比如搞一个图片
// 把一个png格式的转化成data
model.data = UIImagePNGRepresentation([UIImage imageNamed:@“1”]);
// 创建一个可变的data, 初始化归档对象
NSMutableData *data = [NSMutableData data];
// 创建一个归档文件
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
// 进行归档编码
[archiver encodeObject:model forKey:@“JJModel”];
// 编码完成
[archiver finishEncoding];

// 实际上归档, 相当于把编码完的对象, 保存在data中
// 把存有复杂对象data, 写入文件中, 进行持久化
// 寻找路径
NSString *dataPath = [kDocumentPath stringByAppendingPathComponent:@“JJModel.da”];
// 调用写入方话
[data writeToFile:dataPath atomically:YES];
// 释放归档对象
[archiver release];


}

// 反归档, 解码的过程
- (void)unArchiver
{
// data路径
NSString *dataPath = [kDocumentPath stringByAppendingPathComponent:@“JJModel.da”];
// 获取刚才归档的data
NSData *data = [NSData dataWithContentsOfFile:dataPath];
// 创建反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
// 解码返回一个对象
// 这个key, 一定要跟刚才编码的时候一样
JJModel *model [unarchiver decodeObjectForKey:@“JJModel”];
// 反归档完成
[unarchiver finishDecoding];
// 释放反归档对象
[unarchiver release];

}

0 0
原创粉丝点击