3333 @property (nonatomic, assign, readonly) NSUInteger age;
3434 @property (nonatomic, assign, readonly) CYLSex sex;
3535
36- - (instancetype)initWithName:(NSString *)name age:(int )age sex:(CYLSex)sex;
37- + (instancetype)userWithName:(NSString *)name age:(int )age sex:(CYLSex)sex;
36+ - (instancetype)initWithName:(NSString *)name age:(NSUInteger )age sex:(CYLSex)sex;
37+ + (instancetype)userWithName:(NSString *)name age:(NSUInteger )age sex:(CYLSex)sex;
3838
3939 @end
4040
152152 @property (nonatomic, assign, readonly) NSUInteger age;
153153 @property (nonatomic, assign, readwrite) CYLSex sex;
154154
155- - (instancetype)initWithName:(NSString *)name age:(int )age sex:(CYLSex)sex;
156- - (instancetype)initWithName:(NSString *)name age:(int )age;
157- + (instancetype)userWithName:(NSString *)name age:(int )age sex:(CYLSex)sex;
155+ - (instancetype)initWithName:(NSString *)name age:(NSUInteger )age sex:(CYLSex)sex;
156+ - (instancetype)initWithName:(NSString *)name age:(NSUInteger )age;
157+ + (instancetype)userWithName:(NSString *)name age:(NSUInteger )age sex:(CYLSex)sex;
158158
159159 @end
160160```
@@ -285,8 +285,8 @@ atomic属性通常都不会有性能瓶颈。
285285 @property (nonatomic, assign, readonly) NSUInteger age;
286286 @property (nonatomic, assign, readonly) CYLSex sex;
287287
288- - (instancetype)initWithName:(NSString *)name age:(int )age sex:(CYLSex)sex;
289- + (instancetype)userWithName:(NSString *)name age:(int )age sex:(CYLSex)sex;
288+ - (instancetype)initWithName:(NSString *)name age:(NSUInteger )age sex:(CYLSex)sex;
289+ + (instancetype)userWithName:(NSString *)name age:(NSUInteger )age sex:(CYLSex)sex;
290290
291291 @end
292292
@@ -298,7 +298,7 @@ atomic属性通常都不会有性能瓶颈。
298298 CYLUser *copy = [[[self copy] allocWithZone:zone]
299299 initWithName:_name
300300 age:_ age
301- sex: sex ] ;
301+ sex:_ sex ] ;
302302 return copy;
303303}
304304```
@@ -320,8 +320,8 @@ atomic属性通常都不会有性能瓶颈。
320320 @property (nonatomic, assign, readonly) NSUInteger age;
321321 @property (nonatomic, assign, readonly) CYLSex sex;
322322
323- - (instancetype)initWithName:(NSString *)name age:(int )age sex:(CYLSex)sex;
324- + (instancetype)userWithName:(NSString *)name age:(int )age sex:(CYLSex)sex;
323+ - (instancetype)initWithName:(NSString *)name age:(NSUInteger )age sex:(CYLSex)sex;
324+ + (instancetype)userWithName:(NSString *)name age:(NSUInteger )age sex:(CYLSex)sex;
325325 - (void)addFriend:(CYLUser *)user;
326326 - (void)removeFriend:(CYLUser *)user;
327327
@@ -376,7 +376,7 @@ atomic属性通常都不会有性能瓶颈。
376376 CYLUser *copy = [[[self copy] allocWithZone:zone]
377377 initWithName:_name
378378 age:_age
379- sex:sex ];
379+ sex:_sex ];
380380 copy->_friends = [[NSMutableSet alloc] initWithSet:_friends
381381 copyItems:YES];
382382 return copy;
@@ -390,14 +390,14 @@ atomic属性通常都不会有性能瓶颈。
390390
391391【注:深浅拷贝的概念,在下文中有介绍,详见下文的:*** 用@property 声明的NSString(或NSArray,NSDictionary)经常使用copy关键字,为什么?如果改用strong关键字,可能造成什么问题?*** 】
392392
393- 在例子中,存放朋友对象的set是用“copyWithZooe :”方法来拷贝的,这种浅拷贝方式不会逐个复制set中的元素。若需要深拷贝的话,则可像下面这样,编写一个专供深拷贝所用的方法:
393+ 在例子中,存放朋友对象的set是用“copyWithZone :”方法来拷贝的,这种浅拷贝方式不会逐个复制set中的元素。若需要深拷贝的话,则可像下面这样,编写一个专供深拷贝所用的方法:
394394
395395
396396 - (id)deepCopy {
397397 CYLUser *copy = [[[self copy] allocWithZone:zone]
398398 initWithName:_name
399399 age:_age
400- sex:sex ];
400+ sex:_sex ];
401401 copy->_friends = [[NSMutableSet alloc] initWithSet:_friends
402402 copyItems:YES];
403403 return copy;
@@ -409,7 +409,10 @@ atomic属性通常都不会有性能瓶颈。
409409如果抛开本例来回答的话,如下:
410410
411411 - (void)setName:(NSString *)name {
412+ if (_name!=name) {
413+ //[_name release];
412414 _name = [name copy];
415+ }
413416 }
414417
415418
@@ -1051,7 +1054,7 @@ objc在向一个对象发送消息时,runtime库会根据对象的isa指针找
10511054简单来说:
10521055
10531056
1054- > 当使用某对象上的某个方法 ,而该对象上没有实现这个方法的时候,
1057+ > 当该对象上某个方法 ,而该对象上没有实现这个方法的时候,
10551058 可以通过“消息转发”进行解决。
10561059
10571060
0 commit comments