gpt4 book ai didi

swift - 将 nil 作为 UnsafePointer 传递

转载 作者:搜寻专家 更新时间:2023-10-31 08:31:48 24 4
gpt4 key购买 nike

在 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>?.

关于swift - 将 nil 作为 UnsafePointer<UInt8> 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com