美文网首页
NSHomeDirectory()简述

NSHomeDirectory()简述

作者: 懒惰的习惯 | 来源:发表于2016-02-26 16:52 被阅读3671次

    对于app存储方式有多种,在这里我们先介绍NSCoding,也就是解档归档,这种方式是将我们的数据存储在沙盒中的Documents中。
    首先我们利用NSHomeDirectory来获取应用所存储的目录,然后在拼接“Documents”就是我们所用的NSCoding技术需要准备的存和取的路径。而实际上我们可以直接采用[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]来获取路径。

    1.NSHomeDirectory
    NSString *path1 = NSHomeDirectory();
    NSLog(@"path1:%@", path1);path1:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830
    
    
    2.NSSearchPathForDirectoriesInDomains
    NSString *path2 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"path2:%@", path2);path2:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Library/Caches
    
    
    NSString *docPath =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"path3:%@", path3);path3:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Documents
    

    在获取到路径之后我们需要将所存储的对象遵循协议NSCoding,之后才可以使用解档、归档来获取以及存储数据。

    .h文件
    @interface YZPerson : NSObject<NSCoding>
    
    .m文件
    @implementation YZPerson
    // 当将一个自定义对象保存到文件的时候就会调用该方法
    // 在该方法中说明如何存储自定义对象的属性
    // 也就说在该方法中说清楚存储自定义对象的哪些属性
    // 归档
    - (void)encodeWithCoder:(NSCoder *)aCoder
    {
        [aCoder encodeObject:self.name forKey:@"name"];
        [aCoder encodeInteger:self.age forKey:@"age"];
        [aCoder encodeFloat:self.height forKey:@"height"];
    }
    
    // 当从文件中读取一个对象的时候就会调用该方法
    // 在该方法中说明如何读取保存在文件中的对象
    // 也就是说在该方法中说清楚怎么读取文件中的对象
    // 解档
    - (instancetype)initWithCoder:(NSCoder *)aDecoder
    {
        if (self = [super init]) {
            self.name = [aDecoder decodeObjectForKey:@"name"];
            self.age = [aDecoder decodeIntegerForKey:@"age"];
            self.height = [aDecoder decodeFloatForKey:@"height"];
        }
        return self;
    }
    

    在继承YZPerson的类YZStudent中实现解档和归档,只需要重写那两个方法就可以了,并且在.h文件中不需要再次遵循NSCoding协议

    .m文件
    @implementation YZStudent
    // 在子类中重写那两个方法,只添加一个属性的归档,解档
    - (void)encodeWithCoder:(NSCoder *)aCoder
    {
        [super encodeWithCoder:aCoder];
        
        [aCoder encodeFloat:self.weight forKey:@"weight"];
    }
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder
    {
        if (self = [super initWithCoder:aDecoder]) {
            self.weight = [aDecoder decodeFloatForKey:@"weight"];
        }
        return self;
    }
    @end
    

    在外部实现存储以及读取时

    存储
    NSString *stuPath = [docPath stringByAppendingString:@"student.student"];
        // 3. 将自定义的对象保存到文件中
    [NSKeyedArchiver archiveRootObject:person toFile:path];
    
    读取
    NSString *stuPath = [dirPath stringByAppendingString:@"student.student"];
    YZStudent *student = [NSKeyedUnarchiver unarchiveObjectWithFile:stuPath ];
    

    相关文章

      网友评论

          本文标题:NSHomeDirectory()简述

          本文链接:https://www.haomeiwen.com/subject/sirwkttx.html