使用 swift 在 xcode 6 中自定义 NSValueTransformer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25534334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:26:48  来源:igfitidea点击:

Custom NSValueTransformer in xcode 6 with swift

xcodeswiftnsvaluetransformer

提问by marschro

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

有没有人在 xcode 6 beta 中使用 swift 成功实现自定义 NSValueTransformer?

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.

所以这个转换器应该做的就是将 100 添加到 gui 中的给定值。

As you can see, the transformer class appears now in the Value Transformer drop down in IB.

如您所见,转换器类现在出现在 IB 的 Value Transformer 下拉列表中。

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

在 AppDelegate 中注册这个转换器是否正确

override class func initialize() {
  let newTransformer = myTransformer()
}

Does anyone know how this whole stuff should work?

有谁知道这整个东西应该如何工作?

kind regards! martin

亲切的问候!马丁

采纳答案by 042e

After you initialise newTransformeryou should also include the line:

初始化newTransformer后,您还应该包含以下行:

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

Then in your Interface Builder you should use myTransformerinstead of newTransformerunder the Value Transformer dropdown.

然后在您的界面生成器中,您应该在 Value Transformer 下拉列表下使用myTransformer而不是newTransformer

回答by user500

From Xcode release notes:

来自 Xcode 发行说明:

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)

如果您将 NSValueTransformer 的 Swift 子类设置为绑定的值转换器,则 XIB 或故事板将包含对该类的无效引用,并且该绑定在运行时将无法正常工作。您可以在 Value Transformer 字段中输入经过修改的类名,也可以将 @objc(...) 属性添加到 NSValueTransformer 子类来解决此问题。(17495784)

From Swift guide:

来自 Swift 指南:

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.

要使您的 Swift 类在 Objective-C 中可访问和可用,请使其成为 Objective-C 类的后代或使用 @objc 属性标记它。要为在 Objective-C 中使用的类指定一个特定的名称,用@objc(<#name#>) 标记它,其中 <#name#> 是你的 Objective-C 代码将用来引用 Swift 类的名称. 有关 @objc 的更多信息,请参阅 Swift 类型兼容性。

Solution:

解决方案:

Declare your class as @objc(myTransformer) class myTransformer: NSValueTransformerand then you can use "myTransformer" as name...

将您的课程声明为@objc(myTransformer) class myTransformer: NSValueTransformer,然后您可以使用“myTransformer”作为名称...