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);
}

  

posted @ 2017-11-30 11:40  MaricoSun  阅读(183)  评论(0编辑  收藏  举报