Adding a getter makes using an underscore incorrect syntax

前端 未结 3 2145
故里飘歌
故里飘歌 2020-12-11 22:39

I have a class with the following header:

#import 

@interface CustomClass : NSObject

@property (strong, nonatomic) NSString          


        
相关标签:
3条回答
  • 2020-12-11 23:10

    You seem to have two different names for your class. CustomClass and BlogPost. I suggest you make them both one or the other.

    0 讨论(0)
  • 2020-12-11 23:11

    A property is not automatically synthesized if you implement both setter and getter method for that property, so you have to synthesize it explicitly:

    @synthesize foo = _foo;
    

    (or add the instance variable _foo explicitly.)

    The same applies if you implement the getter method for a read-only property.

    (If you implement all necessary accessor methods for a property then the compiler does not assume anymore that this property is necessarily backed up by an instance variable.)

    0 讨论(0)
  • 2020-12-11 23:28

    This is because obj c creates the ivar for you along with a setter and getter whereas you used to have to synthesize the ivar yourself. If you manually create the setter AND getter, however, it assumes that you do not want the ivar and so you then have to synthesize it yourself.

    0 讨论(0)
提交回复
热议问题