【问题标题】:Passing nil as a UnsafePointer<UInt8>将 nil 作为 UnsafePointer<UInt8> 传递
【发布时间】:2017-02-20 03:14:25
【问题描述】:

在 Swift 2.2 中,我能够将 nil 作为有效参数传递给需要 UnsafePointer&lt;UInt8&gt; 的函数。在 Swift 3 中,我不能再这样做了:

func myFuncThatTakesAPointer(buffer: UnsafePointer<UInt8>, length: Int) { /** **/ }

myFuncThatTakesAPointer(buffer: nil, length: 0)
Playground execution failed: error: Xcode8Playground-iOS.playground:62:33: error: nil is not compatible with expected argument type 'UnsafePointer<UInt8>'
myFuncThatTakesAPointer(buffer: nil, length: 0)
                                ^

我现在需要在我的函数中将指针声明指定为可选吗?

【问题讨论】:

标签: swift swift3 unsafe-pointers


【解决方案1】:

我现在需要在我的函数中将指针声明指定为可选吗?

总之,是的。来自release notes

类型 UnsafePointer、UnsafeMutablePointer、AutoreleasingUnsafeMutablePointer、OpaquePointer、Selector 和 NSZone 现在表示不可为空的指针,即永不为零的指针。可空指针现在使用 Optional 表示,例如 UnsafePointer&lt;Int&gt;?

【讨论】:

  • 感谢马特,恭喜获得 200k!
  • 谢谢,但如果你不想,你不必把我放在首位,我会到达那里。 :) 我意识到我的回答有点烦人。 :)))
  • 一切都很好,我显然需要花更多时间阅读语言建议。
  • 实际上,我向您指出的发行说明文档更好。语言建议可能真的很混乱;仅仅因为他们接受了某些东西并不意味着它已在公开版本中实施,或者他们按照提案所述的方式实施。
  • @matt,最好检查两个文档,'cos 提案包含改变的动机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
相关资源
最近更新 更多