在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) ^
我是否需要在我的函数中将指针声明指定为可选?
答案 0 :(得分:4)
我是否需要在我的函数中将指针声明指定为可选?
总之,是的。来自release notes:
类型UnsafePointer,UnsafeMutablePointer,AutoreleasingUnsafeMutablePointer,OpaquePointer,Selector和NSZone现在表示非可空指针 - 即从不为nil的指针。现在使用Optional表示可空指针,例如
UnsafePointer<Int>?
。