Custom NSValueTransformer in xcode 6 with swift
Asked Answered
E

2

17

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.

enter image description here

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

Ecosystem answered 27/8, 2014 at 18:17 Comment(1)
I would guess that it's the swift name mangling. Try declaring it with @objc to get around that.Pasho
R
3

After you initialise newTransformer you should also include the line:

NSValueTransformer.setValueTransformer(newTransformer, forName: "myTransformer")

Then in your Interface Builder you should use myTransformer instead of newTransformer under the Value Transformer dropdown.

Raven answered 15/9, 2014 at 20:7 Comment(1)
This is not the best answer.Symphonist
G
41

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...

Graveclothes answered 27/11, 2014 at 21:18 Comment(3)
This fixed everything for me. Another great resource that helped—the NSHipster article on NSValueTransformer: nshipster.com/nsvaluetransformerIntervale
I was just about to up vote this and realised I had already sometime in the past. ++2Impracticable
thank you! A pity this is not the one marked as the answer!Renshaw
R
3

After you initialise newTransformer you should also include the line:

NSValueTransformer.setValueTransformer(newTransformer, forName: "myTransformer")

Then in your Interface Builder you should use myTransformer instead of newTransformer under the Value Transformer dropdown.

Raven answered 15/9, 2014 at 20:7 Comment(1)
This is not the best answer.Symphonist

© 2022 - 2024 — McMap. All rights reserved.