Existing ivar 'title' for unsafe_unretained property 'title' must be __unsafe_unretained

前端 未结 1 369
小蘑菇
小蘑菇 2020-12-05 13:12

I\'m just getting to grips with Objective-C 2.0

When I try to build the following in Xcode it fails. The error from the compiler is as follows:

Exist

相关标签:
1条回答
  • 2020-12-05 13:57

    I assume you are using ARC.

    Under ARC, the ownership qualification of the property must match the instance variable (ivar). So, for example, if you say that the property is "strong", then the ivar has to be strong as well.

    In your case you are saying that the property is "assign", which is the same as unsafe_unretained. In other words, this property doesn't maintain any ownership of the NSString that you set. It just copies the NSString* pointer, and if the NSString goes away, it goes away and the pointer is no longer valid.

    So if you do that, the ivar also has to be marked __unsafe_unretained to match (if you're expecting the compiler to @synthesize the property for you)

    OR you can just omit the ivar declaration, and just let the compiler do that for you as well. Like this:

    @interface Movie : NSObject
    
    @property(assign) NSString *title;
    @property(assign) int rating;
    @property(assign) int year;
    
    @end
    

    Hope that helps.

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