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

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:

  • UICollectionView with FlowLAyout not scrolling when setting section insets
  • AVAudioPlayer stops playing immediately with ARC
  • Anyway to get string from variable name?
  • Customizing Section indexes in UITableView in iphone application
  • The entitlements specified…profile. (0xE8008016). Error iOS 4.2
  • UITextView link tap recognition is delayed
  • 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?

    Solutions Collect From Internet About “Existing ivar 'title' for unsafe_unretained property 'title' must be __unsafe_unretained”

    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.