#import "ViewController.h"
@interface Person : NSObject
@property (nonatomic,copy)NSString *name;
-(void)print;
@end
@implementation Person
-(void)print
{
NSLog(@"%@",self.name);
}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
id cls = [Person class];
void *obj = &cls;
[(__bridge id)obj print];
// Do any additional setup after loading the view.
}
@end
答案是输出的 <ViewController: 0x7f90df907960>
NSString *str = @"123";
id cls = [Person class];
void *obj = &cls;
[(__bridge id)obj print];
会输出123
这里就两个问题
1.为啥会往上取值
首先cls本身是类对象信息,相当于一个isa指针的作用,而obj指向了cls就是持有了这个isa,而一般调用函数就是通过isa去调用,然后去method_list里面找,而方法列表也确实有这个方法,所以会调用成功