本文介绍了在Swift中添加ValueTransformer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将ValueTransformer添加到可可绑定中.我创建了一个ValueTransformer类,但是在IB中不可见.当作为IB中的ValueTransformer添加时,该应用将在启动时崩溃

I'd like to add a ValueTransformer to a cocoa binding.I've created a ValueTransformer class but it is not visible in IB.When added as ValueTransformer in IB the app will crash at start

class myTransformer: ValueTransformer {

override func valueClassForBinding(_ binding: String) -> AnyClass? {
    return NSString.self
}

override func transformedValue(_ value: Any?) -> Any? {
    return "test"
}


}

我想念什么吗?

谢谢!

推荐答案

首先,将值转换器名称注册到NSValueTransformerName.

First, register your value converter name to NSValueTransformerName.

extension
NSValueTransformerName {
    static let myTransformerName = NSValueTransformerName( rawValue: "myTransformer")
}

然后在程序的最开始即AppDelegate的初始化程序中注册您的值转换器.

Then register your value converter in the very beginning of your program, i.e. AppDelegate's initializer.

@NSApplicationMain class
AppDelegate: NSObject, NSApplicationDelegate {
    override
    init() {
        super.init()
        ValueTransformer.setValueTransformer( myTransformer(), forName: .myTransformerName )
    }
}

https://developer.apple.com/documentation/foundation/valuetransformer

这篇关于在Swift中添加ValueTransformer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 22:45