在Swift中为Transformable属性实现NSValueTransformer [英] Implementing NSValueTransformer for Transformable attributes in Swift
问题描述
我试图实现一个NSValue Transformer,这应该帮助我使用一个Transformable属性保存双核数据到核心数据。
I'm trying to implement a NSValue Transformer, which should help me to save an Double Array into Core Data using a Transformable attribute.
所以我试图实现transformValueClass。但是NSArray.class()被去掉了。不幸的是我没有找到原因。
我的方法看起来像这样
So I tried to implement the transformedValueClass. But NSArray.class() is crossed out. Unfortunately I didn't find a reason for this.
My method looks like this
class PacePerK:NSValueTransformer{
class func transformedValueClass() -> AnyClass!
{
return NSArray.class()
}
}
$ b b
我得到以下编译器错误:
'。'后面的成员名称
类声明中的预期标识符
I get the following compiler errors: Expected member name following '.' Expected identifier in class declaration
t真的帮助我。
为什么NSArray.class划掉了?
如何返回NSArray类而不会导致编译器错误?
Why is the NSArray.class crossed out? How can I return the class of NSArray without causing a compiler error?
推荐答案
Swift中的类对象是:
It appears the correct why to reference class objects in Swift is with:
NSArray.self
此外,在您的示例中,您需要使用override标记convertedValueClass函数。所以:
Also, in your example, you’ll need to mark your transformedValueClass function with "override". So:
class PacePerK:NSValueTransformer{
override class func transformedValueClass() -> AnyClass!
{
return NSArray.self
}
}
这篇关于在Swift中为Transformable属性实现NSValueTransformer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!