我正在将我的应用程序升级到 iOS 10 / swift 3
因此,以这种方式构造一个NSManagedObject
带有字典(从远程数据库接收的值)......
let writerDictionary = [
...: writer.id,
...: writer.name,
...: writer.picture,
...: writer.publicationID,
...: writer.language,
...: writer.country
]
let newWriter = Writer(dictionary: writerDictionary, context: SHARED_CONTEXT)
isFavorite
是我在保存上下文之前构建对象后NSManaged
立即手动设置的属性true
-newWriter.isFavorite = true
newWriter
此行在 iOS 10 中因未捕获的异常而崩溃 - 以前 iOS 9 没有任何问题
我也尝试过newWriter.setValue(true, forKey: "isFavorite")
- 虽然它不会使应用程序崩溃,但布尔值isFavorite
保持false
不变。
有任何想法吗?这是我的 NSManagedObject 的 Writer 子类
import UIKit
import CoreData
@objc(Writer)
class Writer: NSManagedObject {
// Attributes also in the database
@NSManaged var id: NSNumber
@NSManaged var name: String
@NSManaged var picture: String?
@NSManaged var publicationID: NSNumber
@NSManaged var language: String
@NSManaged var country: String
@NSManaged var lastArticle: Date
// Attributes only in the iOS app
@NSManaged var isFavorite: Bool // Initial value is false
@NSManaged var hasNewArticles: Bool
// Relationship objects
@NSManaged var publication: Publication
@NSManaged var articles: [Article]
var writerImage: UIImage? {
get {
return ImageCache.sharedCache.imageWithIdentifier("writers-" + String(id))
}
set {
ImageCache.sharedCache.storeImage(newValue, withIdentifier: "writers-" + String(id))
}
}
override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
super.init(entity: entity, insertInto: context)
}
init(dictionary: [String : AnyObject], context: NSManagedObjectContext) {
let entity = NSEntityDescription.entity(forEntityName: "Writer", in: context)!
super.init(entity: entity, insertInto: context)
id = ...
name = ...
picture = ...
publicationID = ...
language = ...
country = ...
lastArticle = ...
}
override func prepareForDeletion() {
if let _ = writerImage {
writerImage = nil
}
}
}