2016-11-15 56 views
0

我有一个私人BOOL一类为什么我无法直接访问BOOL:_myBool?

@property (nonatomic, assign) BOOL myBool; 

我想覆盖getter和setter,所以我可以进行同步,或将其分派到一个串行队列。

- (BOOL)myBool 
{ 
    __block BOOL test = NO; 
    dispatch_sync(self.shouldShowBannerQueue, ^{ 
     test = _myBool; //This does not work. I get an error "Use of undeclared _myBool" 
    }); 

    return test; 
} 

由于同样的原因,我无法重写setter。我无法直接访问_myBool。

+2

为什么这个标记为[[swift]'? – Alexander

回答

0

如果您正在定义访问器方法,则隐式综合不会发生,您将不会有_myBool变量。但是您可以简单地向该类添加一个实例变量并使用它来支持访问器方法。

+2

或添加'@synthesize myBool;' –

+0

@BorisVerebsky或'@synthesize myBool = _myBool;'等价于隐式行为。就我个人而言,如果我正在编写自定义访问器,我更喜欢添加ivar,因为我觉得'@ synthesize'暗示他们会有默认行为。 – Arkku

相关问题