Custom NSValueTransformer in xcode 6 with swift

后端 未结 2 1332
一向
一向 2021-02-07 07:14

Did anyone successfully implement a custom NSValueTransformer in xcode 6 beta with swift?

I have the following swift class:

import Foundation

class myTr         


        
相关标签:
2条回答
  • 2021-02-07 07:50

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

    0 讨论(0)
  • 2021-02-07 07:54

    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.

    0 讨论(0)
提交回复
热议问题