Skip to content

Commit 3007dea

Browse files
committed
一些小建议
问题1:初始化中的int有无必要改成NSUInteger。 问题2:CYLUser *copy = [[[self copy] allocWithZone:zone] initWithName:_name age:_age sex:sex];sex从何而来? 问题3:- (void)setName:(NSString *)name { if (_name!=name) { //[_name release]; _name = [name copy]; } }苹果这样干是不是效率会高一些?
1 parent 3fc4662 commit 3007dea

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

01《招聘一个靠谱的iOS》面试题参考答案/《招聘一个靠谱的iOS》面试题参考答案(上).md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
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

@@ -152,9 +152,9 @@
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

Comments
 (0)