我正在尝试创建一个全局函数,以便我的核心数据类可以使用此函数来创建自定义 setter 。但我发现“‘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)
}
我有点困惑...
最佳答案
我不确定为什么该引用不明确。 willChangeValueForKey
还有另一种实现,但您的函数声明应该澄清它。
不过,在这种情况下,使该函数通用化对您没有帮助。该函数在非泛型形式中也同样有用,幸运的是,它不存在模糊引用问题。此版本仍然处理 NSManagedObject
实例和子类实例:
func coredataSetter(entity: NSManagedObject, attribute: CustomStringConvertible, value: String) {
entity.willChangeValueForKey(attribute.description)
entity.setPrimitiveValue(value, forKey: attribute.description)
entity.didChangeValueForKey(attribute.description)
}
关于swift - 自定义 setter/getter 核心数据的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34764023/