-2

我正在将我的应用程序升级到 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 = truenewWriter

此行在 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
        }
    }
}
4

2 回答 2

2

解决方案

当前版本的 Swift 3 beta 似乎在处理带有“is”前缀的属性方面存在一些缺陷。

像这样调整属性的名称,解决了它......

 @NSManaged @objc(isFavorite) var isFavorite: Bool
于 2016-07-05T17:20:11.503 回答
2

在 Xcode 8 beta 3 编译器中修复。

从发行说明:

早期的 Swift 3 测试版隐式重命名了名称以“is”开头的 Bool 类型的 @objc 属性的属性、getter 和 setter,以匹配 Objective-C 命名约定。(例如,一个名为 isMagical 的 Swift 布尔属性生成了一个 @property (nonatomic, getter=isMagical) BOOL magic 形式的 Objective-C 声明。)这导致的麻烦比它的价值要多,因此已被删除。(26847223)

于 2016-07-20T20:32:27.323 回答