exc_bad_access(code = 1,address = 0x2012

时间:2012-12-14 19:32:25

标签: objective-c ios cocoa-touch

我收到此错误:

exc_bad_access (code=1, address=0x2012 

当我尝试以这种方式从单身人士那里收到一个值时:

Draggable* sharedSingleton = [Draggable sharedManagerDraggable];
NSLog(@"%@", sharedSingleton.namePassedToDraggable);

上面的代码放在UIImageView类型的Draggable.m中,它也有:

+ (Draggable *)sharedManagerDraggable
{
static Draggable *sharedManagerDraggable = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedManagerDraggable = [[Draggable alloc] init];
});
return sharedManagerDraggable;
}

我以这种方式为namePassedToDraggable分配一个值(来自view.m):

#import Draggable.h

Draggable* sharedSingleton = [Draggable sharedManagerDraggable];
    sharedSingleton.namePassedToDraggable = txt.text;
        NSLog(@"%@", sharedSingleton.namePassedToDraggable);
    dragger.tag = img.tag;

在Draggable.h中我有:

+ (Draggable *) sharedManagerDraggable;

@property (nonatomic, assign) NSString* namePassedToDraggable;

为什么我会收到导致应用崩溃的错误?我在其他viewcontorllers中使用相同的方法,它工作得很好!

1 个答案:

答案 0 :(得分:11)

@property (nonatomic, assign) NSString* namePassedToDraggable;

应该是

@property (nonatomic, strong) NSString* namePassedToDraggable;

如果属性不是strong,则不会保留该属性,并且会在您分配该属性的函数范围的末尾释放它。