iOS学习笔记

Runtime经典面试题

2019-01-18  本文已影响0人  郝嗨森

解析参考链接
(1) 下面的代码输出什么?

@implementation Son : Father
- (id)init {    
    self = [super init];    
    if (self) {        
        NSLog(@"%@", NSStringFromClass([self class]));        
        NSLog(@"%@", NSStringFromClass([super class]));    
}    
    return self;
}
@end

(2) 下面代码的结果?

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...?

@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...?

@interface Sark : NSObject
@property (nonatomic, copy) NSString *name;
@end
@implementation Sark
- (void)speak {    
    NSLog(@"my name's %@", self.name);
}
@end
@implementation ViewController
- (void)viewDidLoad {   
    [super viewDidLoad];    
    id cls = [Sark class];    
    void *obj = &cls;   
    [(__bridge id)obj speak];
}
@end
上一篇下一篇

猜你喜欢

热点阅读