[英]Xcode 10 Swift build error: “Converting non-escaping value to 'T' may allow it to escape”
I'm using the Swift-VectorBoolean library, which is currently on Swift 3.2, not yet updated for Swift 4.2, but should still work on Xcode 10. Running this on Xcode 9, it works fine. 我正在使用Swift-VectorBoolean库,该库当前在Swift 3.2上,尚未针对Swift 4.2进行更新,但仍应在Xcode 10上运行。在Xcode 9上运行该库可以正常工作。 On Xcode 10, it gives an error that I'm not sure how to fix. 在Xcode 10上,它给出了一个我不确定如何解决的错误。 This is the function in question: 这是有问题的功能:
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))
}
The error is on that last line, path.apply
, highlighting the first unsafeBitCast
: 错误在最后一行path.apply
,突出显示了第一个unsafeBitCast
:
Converting non-escaping value to 'T' may allow it to escape
I'm not sure how to modify this code to remove the error, or if this is an issue with Xcode 10. It should be able to compile Xcode 3.2 code.. even updating the code base to Swift 4 in Xcode 9, it still has the same issue. 我不确定如何修改此代码以消除错误,或者这是否是Xcode 10的问题。它应该能够编译Xcode 3.2代码。甚至将代码库更新为Xcode 9中的Swift 4,它仍然有同样的问题。
EDIT: Changing this with the answer from @Vyacheslav allows it to compile, but it gets a runtime error: "Fatal error: Can't unsafeBitCast between types of different sizes" 编辑:使用@Vyacheslav的答案更改它,使其可以编译,但是会遇到运行时错误:“致命错误:不同大小类型之间不能unsafeBitCast”
There is a comment provided above the function which I didn't include in the sample above: 在上面的示例中未包含的功能上方提供了注释:
// 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.
I still don't understand what needs to change here to ensure it builds, and we also don't get a runtime error. 我仍然不明白需要在此处进行哪些更改以确保其构建,并且我们也没有遇到运行时错误。
Use 使用
private func myPathApply(_ path: CGPath!,
block: @escaping @convention(block) (UnsafePointer<CGPathElement>) -> Void) {
}
@noescape
is now by default @noescape
现在默认为
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.