将 nil 作为 UnsafePointer<UInt8> 传递

JAL*_*JAL 4 unsafe-pointers swift swift3

在 Swift 2.2 中,我能够将nil有效参数作为有效参数传入一个需要UnsafePointer<UInt8>. 在 Swift 3 中,我不能再这样做了:

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

myFuncThatTakesAPointer(buffer: nil, length: 0)
Run Code Online (Sandbox Code Playgroud)
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)
                                ^
Run Code Online (Sandbox Code Playgroud)

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

mat*_*att 5

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

一句话,是的。从发行说明

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