《神经病眼中的objc runtime》北京线下分享活动顺利完成,为了配合讲解编造的几个runtime考题发出来分享下:
- 为分享内容配合讲解用,可不是为了面试别人的哦(容易被抽)
- 这几个题分别对应了runtime中几个隐蔽的知识点,挺非主流的,没必要深究
- 答案在本页末尾给出,有同学针对这几道题写了讲解,所以就一笔带过了
- 分享的具体内容争取找个时间写个blog总结下
神经病院objc runtime入院考试
(1) 下面的代码输出什么?
1 2 3 4 5 6 7 8 9 10
| @implementation Son : Father - (id)init { self = [super init]; if (self) { NSLog(@"%@", NSStringFromClass([self class])); NSLog(@"%@", NSStringFromClass([super class])); } return self; } @end
|
(2) 下面代码的结果?
1 2 3 4
| BOOL res1 = [(id)[NSObject class] isKindOfClass:[NSObject class]]; BOOL res2 = [(id)[NSObject class] isMemberOfClass:[NSObject class]]; BOOL res3 = [(id)[Sark class] isKindOfClass:[Sark class]]; BOOL res4 = [(id)[Sark class] isMemberOfClass:[Sark class]];
|
(3) 下面的代码会?Compile Error / Runtime Crash / NSLog…?
1 2 3 4 5 6 7 8 9 10 11
| @interface NSObject (Sark) + (void)foo; @end @implementation NSObject (Sark) - (void)foo { NSLog(@"IMP: -[NSObject (Sark) foo]"); } @end
[NSObject foo]; [[NSObject new] foo];
|
(4) 下面的代码会?Compile Error / Runtime Crash / NSLog…?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @interface Sark : NSObject @property (nonatomic, copy) NSString *name;... |