iOS激情详解之文件夹创建,移动,复制,删除以及归档和反归档

13 篇文章 0 订阅
<span style="font-size:18px;">主要代码精髓:</span>

#import "RootViewController.h"
#import “DXModel.h"

#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define kCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
      [super viewDidLoad];
      [self createFile];
      [self moveFile];
      [self copyFile];
      [self deleteFile];
      [self isExistFild];
      [self unArchive];
      [self unArchive];
}

// 创建一个文件夹    ********************************
- (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 isCreatFile = [manager createDirectoryAtPath:downloadPath withIntermediateDirectories:YES attributes:nil error:nil];

    NSLog(@"%d", isCreatFile);
}

// 移动文件夹    ********************************
- (void)moveFile
{
    // 获取原来路径 定义一个宏
    NSString *oldPath = [kDocumentPath stringByAppendingPathComponent:@"Download"];
    // 获取新路径 library 下的Caches 文件夹 再定义一个宏
    NSString *newPath = [kDocumentPath stringByAppendingPathComponent:@"Download"];
    // 创建文件管理者类的对象(单例对象)
    NSFileManager *manager = [NSFileManager defaultManager];
    // 移动文件夹
    BOOL isMoved = [manager moveItemAtPath:oldPath toPath:newPath error:nil];
   //  NSLog(@"%@", lesson)
    NSLog(@"%d", isMoved);
}

// 复制文件夹    ********************************
- (void)copyFile
{
    // library 下的Caches文件夹 download 复制到document文件夹下
    NSString *oldPath = [kCachesPath stringByAppendingPathComponent:@"download"];
    NSString *newPath = [kDocumentPath stringByAppendingPathComponent:@"download"];
    // 创建文件管理对象
     NSFileManager *manager = [NSFileManager defaultManager];
    //复制
    BOOL isCopy = [manager copyItemAtPath:newPath toPath:oldPath error:nil];
    NSLog(@"%d", isCopy);
    NSLog(@"%@", newPath);
}

// 删除文件夹    ********************************
- (void)deleteFile
{
    // 获取要删除的路径
    NSString *deletePath = [kDocumentPath stringByAppendingPathComponent:@"Download"];
    // 创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];
    // 删除
    BOOL isDelete = [manager removeItemAtPath:deletePath error:nil];
    NSLog(@"%d", isDelete);
}

// 判断一个文件是否存在  ********************************
- (void)isExistFild
{
    NSString *path = [kCachesPath stringByAppendingPathComponent:@"Download"];
    // 创建文件管理对象
    NSFileManager *manager = [NSFileManager defaultManager];
   BOOL isExist = [manager isExecutableFileAtPath:path];
    NSLog(@"%d", isExist);

// 复杂对象归档
- (void)archive
{
    // 归档前需要初始化对象
    DXModel *model = [[DXModel alloc] init];
    // 赋值对象
    model.name = @“DX";
    model.age = 19;
    // 这里可以设置个图片转成data
    // 把一个png格式的转化成data格式
    model.data = UIImagePNGRepresentation([UIImage imageNamed:@"bg_nav"]);
    // 创建一个可变的data 初始化归档对象
    NSMutableData *data = [NSMutableData data];

    // 创建一个归档对象 需要一个可变的就在上面创建一个
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    // 进行归档编码
    [archiver encodeObject:model forKey:@"DXModel"];
    // 编码完成
    [archiver finishEncoding];
    // 实际上归档 相当于把编码完的对象,保存到data中
    NSLog(@"%@",data);

    //把存有复杂对象的data写入文件中 进行持久化
    // 1,搞路径
    NSString *dataPath = [kDocumentPath stringByAppendingPathComponent:@"DXModel.da"];

    // 2,调写入方法
  BOOL is = [data writeToFile:dataPath atomically:YES];
    // 释放对象
    [archiver release];<pre name="code" class="objc">#import “DXModel.h"

@implementation DXModel

- (void)dealloc
{
    [_name release];
    [_data release];
    [super dealloc];
}
//  对复杂对象进行持久化 叫做 归档与反归档(编码与解码)

// 归档方法:编码成可以持久化的格式
- (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;
}
@end

#import “DXModel.h"

@implementation DXModel

- (void)dealloc
{
    [_name release];
    [_data release];
    [super dealloc];
}
//  对复杂对象进行持久化 叫做 归档与反归档(编码与解码)

// 归档方法:编码成可以持久化的格式
- (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;
}
@end

}// 反归档解码过程:给他一个data,给咱一个model- (void)unArchive{ // data路径 NSString *dataPath = [kDocumentPath stringByAppendingPathComponent:@"DXModel.da"]; // 获取刚才归档的对象 NSData *data = [NSData dataWithContentsOfFile:dataPath]; // 创建 反归档对象 NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; // 解码返回一个对象 // 这个Forkey一定要和刚才归档的时候一样 DXModel *model = [unarchive decodeObjectForKey:@"DXModel"]; // 反归档完成 [unarchive finishDecoding]; // 释放反归档对象 [unarchive release]; NSLog(@"-----%@", model.name); UIImage *image = [UIImage imageWithData:model.data]; NSLog(@"------%@",image);}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
 
#import <Foundation/Foundation.h>

 // 复杂对象需要进行持久化 需要遵守一个协议<NSCoding>

@interface DXModel : NSObject<NSCoding>

@property (nonatomic, retain)NSString *name;
@property (nonatomic, assign)NSInteger age;
@property (nonatomic, retain)NSData *data;
@end
#import “DXModel.h"

@implementation DXModel

- (void)dealloc
{
    [_name release];
    [_data release];
    [super dealloc];
}
//  对复杂对象进行持久化 叫做 归档与反归档(编码与解码)

// 归档方法:编码成可以持久化的格式
- (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;
}
@end


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值