相关疑难解决方法(0)

点语法与getter =的方法语法

我不确定这个问题有多少用处,但对我来说似乎很有意思......

我认为使用property/synthesize语句等同于我创建getter/setter.因此

// .h
@property (nonatomic) BOOL on;

// .m
@synthesize on = _on;

// In my mind synthesizes the following methods

// - (BOOL)on;
// - (void)setOn:(BOOL)on;
Run Code Online (Sandbox Code Playgroud)

但是,如果我将声明更改为以下内容:

                              v
@property (nonatomic, getter=isOn) BOOL on;

@synthesize on = _on;

// In my mind synthesizes the following

// - (BOOL)isOn;
// - (void)setOn:(BOOL)on;
Run Code Online (Sandbox Code Playgroud)

然后给出上面的内容我覆盖了getter,所以我知道它何时被调用:

- (BOOL)isOn;
{
    NSLog(@"I was called");
    return _on;
}
Run Code Online (Sandbox Code Playgroud)

现在在实例(myClass)上调用以下内容会导致:

NSLog(@"%d", [myClass isOn]);

//=> 2012-02-09 22:18:04.818 Untitled[1569:707] I was called
//=> 2012-02-09 22:18:04.820 Untitled[1569:707] 1 …
Run Code Online (Sandbox Code Playgroud)

objective-c

12
推荐指数
1
解决办法
903
查看次数

标签 统计

objective-c ×1