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

aur*_*789 4 cocoa objective-c

相同的代码已经在这里受到质疑,但我处理了一个我无法解决的不同问题,可能是因为我是 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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

  @synthesize window;
  @synthesize webber;
Run Code Online (Sandbox Code Playgroud)

谁给了我这么长的错误:

Existing instance variable 'window' for property 'window' with  assign attribute must be __unsafe_unretained
Run Code Online (Sandbox Code Playgroud)

与其他 var "webber" 几乎相同:

Existing instance variable 'webber' for property 'webber' with  assign attribute must be __unsafe_unretained
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助,我真的很感谢 Stackoverflow 社区几天来!

Rak*_*esh 5

ARC 中实例变量的默认所有权限定是strong,就像@robMayoff 提到的assign 一样,unsafe_unretained因此您的代码如下所示:

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

@property (unsafe_unretained) IBOutlet NSWindow *window;
@property (unsafe_unretained) IBOutlet WebView *webber;
Run Code Online (Sandbox Code Playgroud)

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

评论中的链接答案中提供了相同的解决方案。只是补充一些信息。


归档时间:

查看次数:

12657 次

最近记录:

12 年,9 月 前