self.variable和self->变量之间有什么区别? [英] What is the difference between self.variable and self->variable?

查看:69
本文介绍了self.variable和self->变量之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过使用 - >或在iphone sdk中访问变量。一个是最好的?self.variable和self-> variable之间的区别是什么?

I have seen accessing variable in iphone sdk with -> or . symbol.Which one is the best?what is the difference between self.variable and self->variable?

推荐答案

点 - 符号通过访问器,箭头符号直接转到实例变量。试试这段代码:

The dot-notation goes through the accessor, the arrow notation goes directly to the instance variable. Try this code:

@interface Foo : NSObject
@property(assign, nonatomic) NSInteger bar;
@end

@implementation Foo
@synthesize bar;

- (void) setBar: (NSInteger) newBar
{
    NSLog(@"Setting new bar.");
    bar = newBar;
}

- (id) init
{
    self = [super init];
    self->bar = 5; // doesn’t log anything
    self.bar  = 6; // logs
    return self;
}

@end

这篇关于self.variable和self->变量之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆