iOS automatic @synthesize without creating an ivar

后端 未结 3 1940
北恋
北恋 2020-12-09 10:59

If I have a @property which I didn\'t want to have backed via an ivar I simply omitted the @synthesize and had manual getters which re

3条回答
  •  情歌与酒
    2020-12-09 11:37

    If you mark the property as readonly and implement the getter yourself, it seems that iVar will not be created.

    Interface declaration:

    @property (nonatomic, readonly) BOOL myBoolProp;
    

    Impementation:

    - (BOOL)myBoolProp {
        return true;
    }
    

    Trying this:

    - (void)viewDidLoad {
        [super viewDidLoad];
        _myBoolProp = true;
    }
    

    will generate an error: Use of undeclared identifier '_myBoolProp'

    Removing the custom getter method also removes the error, appearing to demonstrate that the iVar has now been generated.

提交回复
热议问题