Swift ObjectMapper 没有调用 didSet

Swift ObjectMapper not calling didSet

我正在使用 ObjectMapper 从某些 JSON 填充我的 Realm 对象的属性。但是 didSet 似乎不会在 remotePath 属性 被 Mapper 更改时被调用。

这是我的 class 的精简版(我刚刚删除了一些属性以保持简洁)

class ImageFile: Object, Mappable  {   

    dynamic var id = 0

    dynamic var filename = ""

    dynamic var remotepath = "" {

        didSet {

            self.filename = self.remotepath.isEmpty ? "x" : "z"

        }

    }


    required convenience init?(map: Map) {

        self.init()

    }    

    override static func primaryKey() -> String? {

        return "id"

    }



    func mapping(map: Map) {

        remotepath <- map[“path"]

    }     

}

我是这样调用的:

let file = ImageFile()
file.id = json["fileId"].int

// Map the properties
file.mapping(map: Map(mappingType: .fromJSON, JSON: json.dictionaryObject ?? [:]))

我已经尝试在 didSet 中放置一个断点以及打印以查看它是否正在触发/但如果我检查数据库,我确实在 remotepath 中看到了正确的值 属性, 肯定是在填.

我知道 didSet 不会在初始化期间触发,但我在初始化之后映射所以它应该触发?我检查了 ObjectMapper 上的 github 个问题,人们已经将 didSet 与 ObjectMapper 一起使用,所以我显然遗漏了一些东西......

非常感谢任何帮助:)

备注:

属性 观察者不在 Realm 对象上工作。这是 Realm 的已知限制,因为对象存在于 Objective-C 运行时(因此是动态修饰符)。有关详细信息,请参阅 this GitHub 问题。您可以使用 Realm 内置通知作为 属性 观察者的解决方法。