gpt4 book ai didi

ios - Xcode 10 Swift 构建错误 : "Converting non-escaping value to ' T' may allow it to escape"

转载 作者:IT王子 更新时间:2023-10-29 05:47:08 25 4
gpt4 key购买 nike

我正在使用 Swift-VectorBoolean 库,它目前在 Swift 3.2 上,尚未针对 Swift 4.2 进行更新,但应该仍可在 Xcode 10 上运行。在 Xcode 9 上运行它,它工作正常。在 Xcode 10 上,它给出了一个我不确定如何修复的错误。这是有问题的功能:

typealias MyPathApplier = @convention(block) (UnsafePointer<CGPathElement>) -> Void
// Note: You must declare MyPathApplier as @convention(block), because
// if you don't, you get "fatal error: can't unsafeBitCast between
// types of different sizes" at runtime, on Mac OS X at least.

private func myPathApply(_ path: CGPath!, block: MyPathApplier) {
let callback: @convention(c) (UnsafeMutableRawPointer, UnsafePointer<CGPathElement>) -> Void = { (info, element) in
let block = unsafeBitCast(info, to: MyPathApplier.self)
block(element)
}

path.apply(info: unsafeBitCast(block, to: UnsafeMutableRawPointer.self), function: unsafeBitCast(callback, to: CGPathApplierFunction.self))
}

错误在最后一行,path.apply,突出第一个 unsafeBitCast:

将非转义值转换为“T”可能允许它转义

我不确定如何修改此代码以消除错误,或者这是否是 Xcode 10 的问题。它应该能够编译 Xcode 3.2 代码。甚至在 Xcode 9 中将代码库更新为 Swift 4 , 它仍然有同样的问题。

编辑:使用@Vyacheslav 的答案更改它允许它编译,但它会出现运行时错误:“ fatal error :无法在不同大小的类型之间进行 unsafeBitCast”

我在上面的示例中没有包含的函数上方提供了一条注释:

// Note: You must declare MyPathApplier as @convention(block), because
// if you don't, you get "fatal error: can't unsafeBitCast between
// types of different sizes" at runtime, on Mac OS X at least.

我仍然不明白这里需要更改什么以确保它能够构建,而且我们也不会收到运行时错误。

最佳答案

使用

private func myPathApply(_ path: CGPath!, 
block: @escaping @convention(block) (UnsafePointer<CGPathElement>) -> Void) {

}

@noescape 现在默认

关于ios - Xcode 10 Swift 构建错误 : "Converting non-escaping value to ' T' may allow it to escape",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579037/

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