3
我已經定義了一個具有計算屬性的類。當我嘗試訪問我的代碼中的屬性時,我得到EXC_BAD_ACCESS
。我在屬性的getter中設置了一個斷點,並注意到它從未被調用過。我不知道是什麼原因造成的。我可以訪問對象的其他屬性。訪問NSManagedObject的計算屬性時的EXC_BAD_ACCESS
下面是代碼
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!)
}
}
}
這是我嘗試訪問的圖像性能,並獲得
執行被中斷,原因是: EXC_BAD_ACCESS(代碼= 1,地址= 0x20的)
當我做actor.image
。 actor
是Person
類的一個對象,並已正確初始化。 我在圖像屬性的getter中放置了一個斷點,它從未被調用過。
if let localImage = actor.image {
cell.actorImageView.image = localImage
} else if actor.imagePath == nil || actor.imagePath == "" {
cell.actorImageView.image = UIImage(named: "personNoImage")
}
我在做什麼錯?
您是否將圖像作爲瞬態屬性添加到CoreData模型中? –
感謝您的回覆!我沒有將圖像添加爲臨時屬性,我希望它是一個計算屬性。我只是想出了這個問題,我沒有在數據模型檢查器中爲實體設置類。 – aneeesh