Core-Data willSave: method

前端 未结 5 694
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 23:01

I have an attribute modificationDate in my Entity A. I want to set its value whenever NSManagedObject is saved. However, if i try to d

5条回答
  •  既然无缘
    2020-12-04 23:05

    Swift 4 solution which is a combination of zmit and Richard answer without the need of recurring to NSNotification:

    override func willSave() {
        let expectedNewValue = "Your new value"
        if customField != expectedNewValue, changedValues()[#keyPath(Entity.customField)] == nil, !isDeleted {
            customField = expectedNewValue
        }
        super.willSave()
    }
    

提交回复
热议问题