从NSKeyedUnarchiver返回的对象的exc_bad_access

时间:2010-10-22 21:08:03

标签: iphone exc-bad-access nscoding

我无法从保存的数据加载符合NSCoding协议的自定义类的实例。我的类有一个UIImage属性,当我为它分配一个新的UIImage时,程序崩溃了一个exc_bad_access。

在视图控制器中,我声明我的对象如下:

@interface SomeViewController : UIViewController  {
    IBOutlet UIImageView *imageView;
    SomeClass *myObject;
 }

@property (nonatomic, retain) SomeClass *myObject;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;

然后在.m文件中使用如下:

@synthesize myObject;
@synthesize imageView;

-(void)viewDidLoad{
  myObject = [NSKeyedUnarchiver unarchiveObjectWithFile:someDataPath];
  imageView.image = myObject.image;
}

到目前为止一切顺利。但后来我选择了一个新图像,并尝试将其设置为myObject的“image”属性,这会抛出一个exc_bad_access

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  [picker dismissModalViewControllerAnimated:YES];
  UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  [imageView setImage:image];
   myObject.image = image;
}

就我单步执行代码而言,它是导致exc_bad_access的最后一行。我究竟做错了什么?

1 个答案:

答案 0 :(得分:2)

你必须保留myObject:

self.myObject = [NSKeyedUnarchiver unarchiveObjectWithFile:someDataPath];