码迷,mamicode.com
首页 > 移动开发 > 详细

ios 归档解档

时间:2017-11-30 13:35:33      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:syn   var   objc   import   sig   unsigned   ati   property   utf8   

.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 归档解档

标签:syn   var   objc   import   sig   unsigned   ati   property   utf8   

原文地址:http://www.cnblogs.com/caicaige/p/7928208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!