Swift 用于自定义setter/getter核心数据的函数

Swift 用于自定义setter/getter核心数据的函数,swift,function,core-data,nsmanagedobject,Swift,Function,Core Data,Nsmanagedobject,我正在尝试创建一个全局函数,以便我的核心数据类可以使用该函数创建自定义设置器。但我得到了“willChangeValueForKey”的模糊用法 我使用的代码是: func coredataSetter<T: NSManagedObject>(entity: T, attribute: CustomStringConvertible, value: String) { entity.willChangeValueForKey(attribute.description)

我正在尝试创建一个全局函数,以便我的核心数据类可以使用该函数创建自定义设置器。但我得到了“willChangeValueForKey”的模糊用法

我使用的代码是:

func coredataSetter<T: NSManagedObject>(entity: T, attribute: CustomStringConvertible, value: String) {
    entity.willChangeValueForKey(attribute.description)
    entity.setPrimitiveValue(value, forKey: attribute.description)
    entity.didChangeValueForKey(attribute.description)
}
func核心数据集(实体:T,属性:CustomStringConverable,值:String){
entity.willChangeValueForKey(attribute.description)
entity.setPrimitiveValue(值,forKey:attribute.description)
entity.didChangeValueForKey(attribute.description)
}

我有点困惑…

我不确定为什么引用不明确。还有另一个
willChangeValueForKey的实现,但是您的函数声明应该澄清它

但是,在这种情况下,将此函数设置为泛型并没有帮助。此函数在非泛型形式中同样有用,幸运的是,它没有模糊引用问题。此版本仍然处理
NSManagedObject
实例和子类实例:

func coredataSetter(entity: NSManagedObject, attribute: CustomStringConvertible, value: String) {
    entity.willChangeValueForKey(attribute.description)
    entity.setPrimitiveValue(value, forKey: attribute.description)
    entity.didChangeValueForKey(attribute.description)
}