类的copy和deepcopy

- (instancetype)initWithName:(NSString *)name

age:(NSUInteger)age

sex:(CYLSex)sex {

if(self = [super init]) {

_name = [name copy];

_age = age;

_sex = sex;

_friends = [[NSMutableSet alloc] init];

}

return self;

}

- (void)setName:(NSString *)name {

_name = [name copy];

}

- (id)copyWithZone:(NSZone *)zone {

CYLUser *copy = [[[self class] allocWithZone:zone]

initWithName:_name

age:_age

sex:_sex];

copy->_friends = [_friends mutableCopy];

return copy;

}

- (id)deepCopy {

CYLUser *copy = [[[self class] allocWithZone:zone]

initWithName:_name

age:_age

sex:_sex];

copy->_friends = [[NSMutableSet alloc] initWithSet:_friends

copyItems:YES];

return copy;

}

上一篇:c# 颜色RGB到HSB互相转换


下一篇:USACO Section 1.3 Prime Cryptarithm 解题报告