Did anyone successfully implement a custom NSValueTransformer in xcode 6 beta with swift?
I have the following swift class:
import Foundation
class myTransformer: NSValueTransformer {
let amount = 100
override class func transformedValueClass() -> AnyClass!
{
return NSNumber.self
}
override func transformedValue(value: AnyObject!) -> AnyObject! {
return value.integerValue + amount
}
}
So all this transformer should do is, adding 100 to a given value in the gui.
As you can see, the transformer class appears now in the Value Transformer drop down in IB.
But if I choose this transformer the application crashes with:
2014-08-27 20:12:17.686 cdTest[44134:303]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Cannot find value transformer with name newTransformer'
Is it right to register this transformer in the AppDelegate with
override class func initialize() {
let newTransformer = myTransformer()
}
Does anyone know how this whole stuff should work?
kind regards! martin
From Xcode release notes:
If you set a Swift subclass of NSValueTransformer as a binding’s value transformer, the XIB or storyboard will contain an invalid reference to the class, and the binding will not work properly at runtime. You can either enter a mangled class name into the Value Transformer field or add the @objc(…) attribute to the NSValueTransformer subclass to solve this problem. (17495784)
From Swift guide:
To make your Swift class accessible and usable back in Objective-C, make it a descendant of an Objective-C class or mark it with the @objc attribute. To specify a particular name for the class to use in Objective-C, mark it with @objc(<#name#>), where <#name#> is the name that your Objective-C code will use to reference the Swift class. For more information on @objc, see Swift Type Compatibility.
Solution:
Declare your class as @objc(myTransformer) class myTransformer: NSValueTransformer
and then you can use "myTransformer" as name...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With