错误:协议需要嵌套类型' _BitsType' (Swift.FloatingPointType)

时间:2016-02-24 08:05:43

标签: swift

我试图让我的一个班级采用FloatingPointProtocol;我已经实现了here所示的所有必需功能,但Swift仍然给出了以下错误:

Protocol requires a nested type '_BitsType'(Swift.FloatingPointType)

我无法找到与_BitsType及其FloatingPointType内嵌套相关的任何文档。为了让我的班级成功采用FloatingPointType,我需要实施什么?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

_BitsType是用于表示浮点值的类型 "原始位"。定义是publicly visible in Swift 1.2。 您可以在Swift open source中查找定义,或使用Swift REPL:

$ swift
Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81). Type :help for assistance.
  1> :type lookup FloatingPointType
protocol FloatingPointType : Strideable {
  typealias _BitsType
  @warn_unused_result static func _fromBitPattern(bits: Self._BitsType) -> Self
  @warn_unused_result func _toBitPattern() -> Self._BitsType
...

这两个方法在浮点值和。之间进行转换 一些具有相同位模式的整数类型。例如,它是 UInt32的{​​{1}}:

$ swift
Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81). Type :help for assistance.
  1> :type lookup Float
...
extension Float : FloatingPointType {
  typealias _BitsType = Swift.UInt32
  static func _fromBitPattern(bits: Swift.Float._BitsType) -> Swift.Float
  func _toBitPattern() -> Swift.Float._BitsType
...

对于你自己的浮点类型,你必须决定一个合适的 整数类型,可以表示原始位,并提供 转换功能。对于"短暂浮动"格式可以 例如:

Float

然而, struct MyFloat : FloatingPointType { let value : UInt16 func _toBitPattern() -> UInt16 { return value } static func _fromBitPattern(bits: UInt16) -> MyFloat { return MyFloat(value: bits) } // .... } 和转换函数都没有 在Swift 2(或更高版本)中公开显示,而不是正式显示 记录。这可能表明实施自己的 不支持浮点类型,可能无法正常工作。

相关问题