Semantic Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects

前端 未结 10 1244
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 06:54

I\'m currently using the iOS 5 SDK trying to develop my app. I\'m trying to make an NSString a property, and then to synthesize it in the .m file (I have done this before wi

10条回答
  •  醉酒成梦
    2020-12-07 07:29

    It doesn't look like what Bavarious was suggesting was what you wanted to do. All you want to do is declare an instance variable NewTitle and then synthesize the property. We used to have to declare the instance variable and property. No more.

    Now, I believe the right way of doing this is the following:

    .h

    @interface ViewController : UIViewController
    
    @property (nonatomic, strong) NSString *newTitle;
    

    .m

    @synthesize newTitle = _newTitle; // Use instance variable _newTitle for storage
    

    The instance variable for the property newTitle is synthesized. You don't want your instance variable to be the same as your property - too easy to make mistakes.

    See Example: Declaring Properties and Synthesizing Accessors

提交回复
热议问题