English 中文(简体)
EXC_BAD_INSTRUCTION IBOutlet NSWindow *window
原标题:EXC_BAD_INSTRUCTION when synthesizing @property (weak) IBOutlet NSWindow *window

I m a newbie to ObjC/Cocoa and Mac development in general, and toying with the basics.

狮子角的Xcode 4.2中新的可可应用的模拟缺省模板就是这样:

// AppDelegate.h
#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@end



// Appdelegate.m
#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

@end

我先把这一基础作为各种实验的基础。 阅读自动参考数字(该项目将使用)——,例如 我假定,也许甚至可以(should)。 替换<代码>assign <代码>NSWindow*window with weak,但似乎没有。

服用的是罚款,但在开张时用<条码>hang。 图1:方案收到信号:“EXC_BAD_INSTRUCTION”,载于AppDelegate.m,其行文为@synthesizewindow;

将限定语改为<代码>strong 进行方案工作,但我看不出从<条码>到<条码>到<条码>的意思。 下面的印象是,非阿经共体/非经共体的配对物是分配/分配的,保留/保留。

一位经验更丰富的密码朋友建议,即使“weak 等成因window被过早地处理,而且有些准入尝试失败,该例外应当为EXC_BAD_ACCESS,而不是EXC_BAD_INSTRUCTION

我在这里显然没有东西,但我不知道什么。

http://www.un.org。 在对坠毁时间的 g果进行更仔细的研究之后,同一位朋友向我指出:,Mike Ash 的文章对此做了一些说明。 由于我的理解之外的原因,NSWindow和其他一些高于retain的类别。 不能把提及不力作为目标。 有趣的是,将财产申报改为这项工作:

@property (unsafe_unretained) IBOutlet NSWindow *window;

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/ chapters/ocProperties.html 申报不动产的 Apple果文件

With that, a REVISED QUESTION:

What would be the proper way to go here? Stick to assign despite mentions around the web that it shouldn t be used with ARC? Go for strong? Keep using unsafe_unretained since it seems to work? Something else?

最佳回答

Conceptually, weak is the correct qualifier for a top-level IBOutlet on OS X (iOS is another story). However, to create a proper weak reference that zeroes on deallocation requires cooperation from the Objective C runtime. Classes that override retain or release break this support and so you can t create a weak reference to them. UIWindow is one such class.

因此,该模板使用了分配。 也许,如果能够让非洲排雷中心,它应该真正使用不安全的同义词。 无论在哪一种情况下,你都有一个简单的软弱提法,但这种提法并非零。

问题回答

Mike Ash s blog discusses the issue with some Cocoa classes.

Look for it in the middle part of the page: Friday Q&A ARC. Look/Search for the text that starts with "ARC s implementation of zeroing weak references..."

问题是,有些班级没有处理......weak带出的零点。 解决办法是采用ARC正常模板提供的assign<>。

Well, to answer the second question, even Apple s templates use assign for window when using ARC. So you may be safe for now. But your mileage may vary in the future.





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...