美文网首页
Runtime妙用之-归档

Runtime妙用之-归档

作者: 南少cc | 来源:发表于2017-11-29 19:12 被阅读0次
    archive.jpg

    对于数据存储的归档方式,实现NSCoding协议

    - (void)encodeWithCoder:(NSCoder *)aCoder
    - (id)initWithCoder:(NSCoder *)aDecoder

    直接上代码以UserModel说明,该类有定义两个属性name,gender。

    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSString *gender;
    @end
    

    .m文件中文件中用常见方法实现NSCoding协议

    - (void)encodeWithCoder:(NSCoder *)aCoder{
        [aCoder encodeObject:self.name forKey:@"name"];
        [aCoder encodeObject:self.gender forKey:@"gender"];   
    }
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder{
        self = [super init];
        if (self) {
            self.name = [aDecoder decodeObjectForKey:@"name"];
            self.gender = [aDecoder decodeObjectForKey:@"gender"];
        }
        return self;
    }
    

    对于有少量属性的这样写还凑合,但是对于拥有大量属性的类来说,这样就要做很多体力活了,费时费力!!!然鹅----苹果daddy还是给了更方便的实现方法,那就是Runtime登场了!见证奇迹的时刻到了!-------------------

    对于归档咱可以这样来写:

    /**
     *  归档 */
    - (void)encodeWithCoder:(NSCoder *)aCoder
    {
        unsigned int count;
        // 获得指向当前类的所有属性的指针
        objc_property_t *properties = class_copyPropertyList([self class], &count);
        for (int i = 0; i < count; i++) {
            // 获取指向当前类的一个属性的指针
            objc_property_t property = properties[i];
            // 获取C字符串属性名
            const char *name = property_getName(property);
            // C字符串转OC字符串
            NSString *propertyName = [NSString stringWithUTF8String:name];
            // 通过关键词取值
            NSString *propertyValue = [self valueForKeyPath:propertyName];
            // 编码属性
            [aCoder encodeObject:propertyValue forKey:propertyName];
        }
        free(properties);
    }
    

    对于解档咱可以这样来写:

    /**
     解档
     */
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        if(self = [super init])
        {
            unsigned int count;
            // 获得指向当前类的所有属性的指针
            objc_property_t *properties = class_copyPropertyList([self class], &count);
            for (int i = 0; i < count; i++) {
                // 获取指向当前类的一个属性的指针
                objc_property_t property = properties[i];
                // 获取C字符串的属性名
                const char *name = property_getName(property);
                // C字符串转OC字符串
                NSString *propertyName = [NSString stringWithUTF8String:name];
                // 解码属性值
                NSString *propertyValue = [aDecoder decodeObjectForKey:propertyName];
                [self setValue:propertyValue forKey:propertyName];
            }
            // 记得释放
            free(properties);
        }
        return self;
    }
    

    对于相同的部分也可以再提取,这里就不整了--🙃🙃🙃🙃🙃🙃

    接下来就是测试了:

    UserModel *model = [[UserModel alloc] init];
        model.name = @"haha";
        model.gender = @"未知";
        //获取文件目录
        NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory = [documentDirectories objectAtIndex:0];
        NSLog(@"documentDirectory = %@",documentDirectory);
        documentDirectory = [documentDirectory stringByAppendingPathComponent:@"userModel.archive"];
        //自定义对象归档存到文件中
        [NSKeyedArchiver archiveRootObject:model toFile:documentDirectory];
        //解档:
        UserModel* p = [NSKeyedUnarchiver unarchiveObjectWithFile:documentDirectory];
        NSLog(@"name = %@,gender = %@",p.name,p.gender);
    

    完毕!😑😑😑😑😑😑😑😑

    相关文章

      网友评论

          本文标题:Runtime妙用之-归档

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