访问NSManagedObject的计算属性时的EXC_BAD_ACCESS

时间:2015-06-25 14:05:18

标签: ios swift exc-bad-access

我已经定义了一个具有计算属性的类。当我尝试在我的代码中访问该属性时,我得到EXC_BAD_ACCESS。我在属性的getter中设置了一个断点,并注意到它从未被调用过。我不知道造成这种情况的原因。我可以访问该对象的其他属性。

这是代码

import UIKit
import CoreData

@objc(Person)

class Person: NSManagedObject {

    struct Keys {
        static let Name = "name"
        static let ProfilePath = "profile_path"
        static let Movies = "movies"
        static let ID = "id"
    }

    @NSManaged var name: String
    @NSManaged var id: NSNumber
    @NSManaged var imagePath: String?
    @NSManaged var movies: [Movie]

    override init(entity: NSEntityDescription, insertIntoManagedObjectContext     context: NSManagedObjectContext?) {
    super.init(entity: entity, insertIntoManagedObjectContext: context)
}



init(dictionary: [String : AnyObject], context: NSManagedObjectContext) {

    let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)!

    super.init(entity: entity, insertIntoManagedObjectContext: context)



    name = dictionary[Keys.Name] as! String
    id = dictionary[Keys.ID] as! Int
    imagePath = dictionary[Keys.ProfilePath] as? String
}

var image: UIImage? {
    get {
        return TheMovieDB.Caches.imageCache.imageWithIdentifier(imagePath)
    }

    set {
        TheMovieDB.Caches.imageCache.storeImage(image, withIdentifier: imagePath!)
    }
}
}

这是我尝试访问image属性并获取

的方法
  

执行被中断,原因:   EXC_BAD_ACCESS(代码= 1,地址= 0x20)

当我做actor.image时。 actorPerson类的对象,已正确初始化。 我在getter中为image属性设置了一个断点,它从未被调用过。

if let localImage = actor.image {
            cell.actorImageView.image = localImage
        } else if actor.imagePath == nil || actor.imagePath == "" {
            cell.actorImageView.image = UIImage(named: "personNoImage")
    }

我做错了什么?

1 个答案:

答案 0 :(得分:3)

我刚刚发现了问题,我没有为数据模型检查器中的实体设置类。这解决了它