Reason to use ivars vs properties in objective c

前端 未结 5 1434
说谎
说谎 2020-11-27 05:50

I have been unable to find any information on this topic and most of what I know about it has come by complete accident (and a few hours of trying to figure out why my code

5条回答
  •  北海茫月
    2020-11-27 06:16

    This may be an old question.. but in "modern times", @synthesize- is NOT necessary.

    @interface       SomeClass : NSObject
    @property NSString * autoIvar;
    @end
    @implementation  SomeClass
    - (id) init { return self = super.init ? _autoIvar = @"YAY!", self : nil; }
    @end
    

    The _underscored backing ivar IS synthesized automatically... and is available within THIS class' implementation, directly (ie. without calling self / calling the automatically generated accessors).

    You only need to synthesize it if you want to support subclass' ability to access the _backingIvar (without calling self), or for myriad other reasons, described elsewhere.

提交回复
热议问题