Objective-C现有的实例变量必须是__unsafe_unretained错误

时间:2013-03-26 01:43:01

标签: objective-c cocoa

这里已经提出了相同的代码问题,但是我处理了一个我无法解决的问题,可能是因为我是Objective-C的新手,所以我决定提问:)

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

所以,在webberAppDelegate.m中,我想这是我的问题:

  @synthesize window;
  @synthesize webber;

谁给我这个长期错误:

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

和其他var“webber”的实际情况相同:

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

感谢您的帮助,我非常感谢Stackoverflow社区!

1 个答案:

答案 0 :(得分:2)

ARC中实例变量的默认所有权限定为strong,与@robMayoff一样,提及的赋值与unsafe_unretained相同,因此您的代码如下所示:

@interface webberAppDelegate : NSObject <NSApplicationDelegate> {
   __strong NSWindow *window;
   __strong WebView *webber;
}

@property (unsafe_unretained) IBOutlet NSWindow *window;
@property (unsafe_unretained) IBOutlet WebView *webber;

如@Firoze提供的链接答案中所述,财产声明和iVar应具有匹配的所有权资格。因此,解决方案是将上面代码中的__strong设置为__unsafe_unretained,或者完全删除实例变量声明,以便编译器处理它。

评论中的链接答案中提供了相同的解决方案。只需添加一些信息。