Objective c 不安全\u未恢复vs弱:setDelegate期间崩溃
我对不安全的\u未恢复的和弱的关键字有一些疑问:作为一种读取,它们是完全相同的,唯一的区别是,如果释放指向的对象,弱的一个设置为空 现在我修改了下面的代码,它在[instanceOfTheView setDelegate:self]期间在点[#2]处崩溃 但是如果在I4vMainView声明中[1]我替换Objective c 不安全\u未恢复vs弱:setDelegate期间崩溃,objective-c,cocoa,Objective C,Cocoa,我对不安全的\u未恢复的和弱的关键字有一些疑问:作为一种读取,它们是完全相同的,唯一的区别是,如果释放指向的对象,弱的一个设置为空 现在我修改了下面的代码,它在[instanceOfTheView setDelegate:self]期间在点[#2]处崩溃 但是如果在I4vMainView声明中[1]我替换 @property (nonatomic, weak) id <I4vDraggingFileProt> delegate; @属性(非原子,弱)id委托; 与 @proper
@property (nonatomic, weak) id <I4vDraggingFileProt> delegate;
@属性(非原子,弱)id委托;
与
@property(非原子的、不安全的\u未维护的)id委托;
它工作得很好。这种行为的原因是什么?谢谢
详细信息:使用ARC编译目标10.7。代码4.5.2。苹果LLVM4.1
在I4vMainView类中,我有:
//----------- I4vMainView.h --------
@protocol I4vDraggingFileProt <NSObject>
-(void) anURLWasDeopped: (NSURL *) droppedUrl;
@end
@interface I4vMainView : NSView <NSDraggingDestination>{
NSImageCell *imageCell;
NSImage * image;
}
@property (nonatomic, weak) id <I4vDraggingFileProt> delegate; // [#1]
/------------I4vMainView.h--------
@协议I4vDraggingFileProt
-(void)anURLWasDeopped:(NSURL*)droppedUrl;
@结束
@接口I4vMainView:NSView{
NSImageCell*imageCell;
NSImage*图像;
}
@属性(非原子,弱)id委托;//[#1]
在打电话的时候
//----------- I4vViewController.h --------
@class I4vMainView;
@protocol I4vDraggingFileProt <NSObject>
-(void) anURLWasDeopped: (NSURL *) droppedUrl;
@end
@interface I4vViewController : NSViewController <I4vDraggingFileProt>{
I4vMainView * mv;
}
-(void) anURLWasDeopped: (NSURL *) droppedUrl;
@end
//----------- I4vViewController.m --------
@implementation I4vViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
- (void)loadView{
mv = [[I4vMainView alloc] init];
[mv setDelegate:self]; // <-- [#2]
[self setView:mv];
}
-(void) anURLWasDeopped: (NSURL *) droppedUrl{
// ...
}
@end
/------------I4vViewController.h--------
@I4vMainView类;
@协议I4vDraggingFileProt
-(void)anURLWasDeopped:(NSURL*)droppedUrl;
@结束
@接口I4vViewController:NSViewController{
I4vMainView*mv;
}
-(void)anURLWasDeopped:(NSURL*)droppedUrl;
@结束
//-----------I4vViewController.m--------
@I4vViewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//这里是初始化代码。
}
回归自我;
}
-(void)负荷视图{
mv=[[I4vMainView alloc]init];
[mv setDelegate:self];//我找到了答案:如中所述
当前无法创建对以下类(emphasis mine)实例的零化弱引用:
nStasePosiTeter、NStPosiNS、NSFOnMeNoStor、NSfFoundSub、nSimige、NSnMeNuVIEW、nSabaGrApHythScript、NStimeLealEngOnTealTeaServer、NStabeLeVIEW、NSTEXVIEW、NSVIEW控制器、NSWDIW和NSWIDWORKER。此外,在OS X中,AV基础框架中没有类支持弱引用。
对于声明的属性,您应该使用assign而不是weak;对于变量,您应该使用_safe_unrepaired而不是_weak
此外,不能从ARC下的NSHashTable、NSMapTable或NSPointerArray实例创建弱引用
不安全
和弱
之间的区别在于弱
引用在被引用对象被解除分配时设置为nil
,而不是在被引用对象被释放时。区别很大。另外,弱
需要iOS 5.0或更高版本。不安全
在iOS 4.3中工作。我不知道我不确定什么版本的OS X支持weak
。难道你不能在loadView中打印mv的值吗?用“released”这个词,我的意思是解除分配,用“error”这个词。我用Cocoa编码,weak关键字对我所有的用户都非常有效IBoutlets@RamyAlZuhouri“只打印值”是什么意思?指针不为null,且alloc和int为空successful@Sabby请再次阅读我的评论。我说iOS在iOS 5.0及更高版本中支持弱。我说我不知道什么版本的OS X支持弱。
//----------- I4vViewController.h --------
@class I4vMainView;
@protocol I4vDraggingFileProt <NSObject>
-(void) anURLWasDeopped: (NSURL *) droppedUrl;
@end
@interface I4vViewController : NSViewController <I4vDraggingFileProt>{
I4vMainView * mv;
}
-(void) anURLWasDeopped: (NSURL *) droppedUrl;
@end
//----------- I4vViewController.m --------
@implementation I4vViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
- (void)loadView{
mv = [[I4vMainView alloc] init];
[mv setDelegate:self]; // <-- [#2]
[self setView:mv];
}
-(void) anURLWasDeopped: (NSURL *) droppedUrl{
// ...
}
@end
@property (nonatomic, weak) id <I4vDraggingFileProt> delegate;
<I4vMainView: 0x10060bf40> objc[4773]: cannot form weak reference to instance (0x10061bf10) of class I4vViewController