核心数据:在实体ZZZZ上找不到属性YYYY的类名称为XXX的NSValueTransformer [英] Core Data : No NSValueTransformer with class name XXX was found for attribute YYYY on entity ZZZZ
问题描述
我在xcdatamodel文件中设置了CoreData模型。
I have my CoreData model set in a xcdatamodel file.
我的属性YYYY具有可转换的类型,我在数据模型检查器中设置了转换器名称。
My attribute YYYY has a type transformable and I set the tranformer name in the Data model inspector.
我的情况是我在模型中存储了 [CLLocation]
。
I my case I was storing a [CLLocation]
in my model.
class LocationArrayTransformer : NSValueTransformer {
override func transformedValue(value: AnyObject?) -> AnyObject? {
let locations = value as! [CLLocation]
return NSKeyedArchiver.archivedDataWithRootObject(locations)
}
override func reverseTransformedValue(value: AnyObject?) -> AnyObject? {
let data = value as! NSData
return NSKeyedUnarchiver.unarchiveObjectWithData(data)
}
}
那是我的价值转换器。
That's my value transformer.
但是我仍然会在控制台中收到警告:在实体ZZZZ上没有为属性YYYY找到类名称为XXX的NSValueTransformer
But somehow I'm still getting the warning in the console : No NSValueTransformer with class name XXX was found for attribute YYYY on entity ZZZZ
任何想法为何?
推荐答案
我花了很多时间在此上,以不分享我发现的解决方案:
I spent way to much time on this to not share the solution I found :
我必须将 NSValueTransformer
子类提供给Objc。
I had to make the NSValueTransformer
subclass available to Objc.
@objc(LocationArrayTransformer)
class LocationArrayTransformer : NSValueTransformer {
....
}
就这么简单。
@Sbooth指出,Swift类具有名称空间。使用 @objc
可使类可用而无需命名间隔。因此,将转换器名称设置为 MyApp.Mytransformer
也可以!
As @Sbooth points out, Swift classes are namespaced. Using @objc
makes the class available without namespacing. So setting the transformer name as MyApp.Mytransformer
works well too !
这篇关于核心数据:在实体ZZZZ上找不到属性YYYY的类名称为XXX的NSValueTransformer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!