ios 归档解档

Posted Marico

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios 归档解档相关的知识,希望对你有一定的参考价值。

.h文件

#import <Foundation/Foundation.h>

typedef void(^myBlock)(NSData *);

typedef NS_ENUM(NSInteger, CYLGender){
    CYLGenderMail,
    CYLGenderFemale
};

@interface User : NSObject<NSCopying,NSMutableCopying, NSCoding>
{
    unsigned int count;
}

@property (nonatomic, readwrite, copy) NSString *name;
@property (nonatomic, readwrite, assign) NSUInteger age;
@property (nonatomic, readwrite, assign) CYLGender gender;
@end

.m文件

#import "User.h"
#import <objc/runtime.h>

@interface User()

@end

@implementation User

@synthesize firstName = _firstName;
@synthesize lastName = _lastName;
 
- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

- (instancetype)initWithName:(NSString *)name
                         age:(NSUInteger)age
                      gender:(CYLGender)gender {
    self = [super init];
    if (self) {
        _name = name;
        _age = age;
        _gender = gender;
    }
    return self;
}



- (void)addFriend:(User *)user {
    [_friends addObject:user];
}

- (void)removeFriend:(User *)user {
    [_friends removeObject:user];
}

- (id)copyWithZone:(NSZone *)zone {
    User *copy = [[[self class] allocWithZone:zone] initWithName:_name age:_age gender:_gender];
    copy -> _friends = [_friends mutableCopy];
    return copy;
}

- (id)deepCopy {
    User *copy = [[[self class] alloc] initWithName:_name
                                                age:_age
                                             gender:_gender];
    copy -> _friends = [[NSMutableSet alloc] initWithSet:_friends copyItems:YES];
    return copy;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    User *copy = [[[self class] allocWithZone:zone] initWithName:_name age:_age gender:_gender];
    copy -> _friends = [_friends mutableCopy];
    return copy;
}

#pragma mark - 解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    unsigned int outCount;
    if (self == [super init]) {
        
        objc_property_t * objs = class_copyPropertyList([self class], &outCount);
        for (int i =0; i < outCount; i++) {
            objc_property_t obj = objs[i];
            NSString *name = [NSString stringWithUTF8String:property_getName(obj)];
            id value = [aDecoder decodeObjectForKey:name];
            [self setValue:value forKey:name];
        }
        free(objs);
        
//        Ivar *ivars = class_copyIvarList([self class], &outCount);
//        for (int i =0; i < outCount; i ++) {
//            Ivar ivar = ivars[i];
//            NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];
//            id value = [aDecoder decodeObjectForKey:keyName];
//            [self setValue:value forKey:keyName];
//        }
//        free(ivars);
    }
    return self;
}

#pragma mark - 归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
    unsigned int outCount;
    objc_property_t *objcs = class_copyPropertyList([self class], &outCount);
    for (int i=0; i<outCount; i++) {
        objc_property_t objc = objcs[i];
        NSString *name = [NSString stringWithUTF8String:property_getName(objc)];
        id value = [self valueForKey:name];
        [aCoder encodeObject:value forKey:name];
    }
    free(objcs);
    
    
//    Ivar *ivars = class_copyIvarList([self class], &outCount);
//    for (int i =0; i <outCount; i++) {
//        Ivar ivar = ivars[i];
//        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];
//        if ([keyName isEqualToString:[self ignoreParameter]]) {
//            continue;
//        }
//        id value = [self valueForKey:keyName];
//        [aCoder encodeObject:value forKey:keyName];
//    }
//    free(ivars);
}

  



以上是关于ios 归档解档的主要内容,如果未能解决你的问题,请参考以下文章

iOS 归档解档

iOS解档与归档

iOS 数据的归档(NSKeyedArchiver )与数据的解档(NSKeyedUnarchiver) 加密 提高安全性

Swift 使用Runtime对模型进行归档解档

面试iOS 开发面试题(三)

用runtime来重写Coder和deCode方法 归档解档的时候使用