您的位置:首页 > 移动开发 > Objective-C

[Objective-C] @property是不能被“预处理”的,而是直接编译成汇编指令

2015-07-07 07:27 344 查看
参考:http://stackoverflow.com/questions/31241643/how-can-i-get-direcitve-preprocessed-objective-c-code

对于下面的Objc代码,我们想看到@property被“预处理”之后长什么样子

// ========= Person.h =========
@interface Person: NSObject
{
}
-(void) Print;
@property int age;
@end

// ========= Person.m =========
@implementation Person
-(void) Print
{
NSLog(@"Print_Age:%d", _age);
}
@end
我们期望看到它是长这样的:

// ========= Person.h =========
@interface Person: NSObject
{
int _age;
}
-(void) Print;
-(int) age;
-(void) setAge:(int) age;
@end

// ========= Person.m =========
@implementation Person
-(void) Print
{
NSLog(@"Print_Age:%d", _age);
}
-(int) age {
return _age;
}
-(void) setAge:(int) age {
_age = age;
}
@end
事实上,最终的代码确实可以理解成是上面那样的。但是要注意的是,@property、@synthesize 这样的东西不像 #define xxx yyy 是可以被编译器预处理的,@property、@synthesize 会被编译器直接编译成汇编代码,不存在“预处理中间代码”阶段,因此我们不可能看到上面那样的“中间代码”。就好比 for/while/switch/case 这样的最基本的关键字,它们最终是怎么工作的,编译器不会通过中间代码的形式呈现给用户,而是直接编译成汇编指令。

如果一定要深究最终编译器做了些什么,它把 @property、@synthesize 是怎么处理的,可以在 XCode 中点击菜单中的 Product -> Perform Action -> Assemble XXX 来查看最终的汇编代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: