Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

objective-c

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:

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

// main.m #import <Foundation/Foundation.h> #import "Movie.h" int main (int argc, const char * argv[]){     Movie *movie = Movie.new;      NSLog(@"%@", movie);      return 0; }  // movie.h #import <Foundation/Foundation.h>  @interface Movie : NSObject{     NSString *title;     int year;     int rating; }  @property(assign) NSString *title; @property(assign) int rating; @property(assign) int year;  @end  #import "Movie.h"  @implementation Movie;  @synthesize title; // this seems to be issue - but I don't understand why? @synthesize rating; @synthesize year;  @end 

Can anybody explain where I've gone wrong?

like image 433
bodacious Avatar asked Dec 03 '11 13:12

bodacious


1 Answers

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.

like image 170
Firoze Lafeer Avatar answered Oct 11 '22 13:10

Firoze Lafeer