ios 歸檔解檔

來源:互聯網
上載者:User

標籤: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 歸檔解檔

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.