Objective-C existing instance variable must be __unsafe_unretained error

The same code is already set here, but I am dealing with another problem that I cannot solve on my own because I am new to Objective-C, so I decided to ask a question :)

webberAppDelegate.h:

#import <Cocoa/Cocoa.h> #import <WebKit/WebKit.h> @interface webberAppDelegate : NSObject <NSApplicationDelegate> { NSWindow *window; WebView *webber; } @property (assign) IBOutlet NSWindow *window; @property (assign) IBOutlet WebView *webber; @end 

webberAppDelegate.m:

 #import "webberAppDelegate.h" @implementation webberAppDelegate @synthesize window; @synthesize webber; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSString *urlString = @"http://www.apple.com"; // Insert code here to initialize your application [[webber mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]]; } @end 

So, in webberAppDelegate.m, here is my problem with this fraction, I suppose:

  @synthesize window; @synthesize webber; 

who gave me this long error:

 Existing instance variable 'window' for property 'window' with assign attribute must be __unsafe_unretained 

and pratic is the same for another var "webber":

 Existing instance variable 'webber' for property 'webber' with assign attribute must be __unsafe_unretained 

Thanks for your help, I really appreciate the Stackoverflow community for a few days!

+4
source share
1 answer

The default owner qualification for instance variables in ARC is strong , and like @robMayoff, the assignment mentioned is the same as unsafe_unretained , so your code reads like this:

 @interface webberAppDelegate : NSObject <NSApplicationDelegate> { __strong NSWindow *window; __strong WebView *webber; } @property (unsafe_unretained) IBOutlet NSWindow *window; @property (unsafe_unretained) IBOutlet WebView *webber; 

As mentioned in the linked answer provided by @Firoze, the property declaration and iVar must have the appropriate property qualification. So the solution would be to make __strong in the __unsafe_unretained code __unsafe_unretained or completely remove the instance variable declarations so that the compiler takes care of that.

The same solution is contained in the linked answer in the comment. Just add some info.

+2
source

All Articles