将 nil 作为 UnsafePointer<UInt8>

Passing nil as a UnsafePointer<UInt8>

在 Swift 2.2 中,我能够将 nil 作为有效参数传递给需要 UnsafePointer<UInt8> 的函数。在 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)
                                ^

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

Am I required to specify the pointer declaration in my function as optional now?

一句话,是的。来自 release notes:

The types UnsafePointer, UnsafeMutablePointer, AutoreleasingUnsafeMutablePointer, OpaquePointer, Selector, and NSZone now represent non-nullable pointers—that is, pointers that are never nil. A nullable pointer is now represented using Optional, for example, UnsafePointer<Int>?.