我正在使用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
:
Converting non-escaping value to 'T' may allow it to escape
我不确定如何修改此代码以消除错误,或者这不是Xcode 10的问题。它应该能够编译Xcode 3.2代码。甚至将代码库更新为Xcode 9中的Swift 4。 ,它仍然有相同的问题。
编辑:使用@Vyacheslav的答案更改它,使其可以编译,但会遇到运行时错误: “致命错误:不能在不同大小的类型之间使用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.
我仍然不知道需要在此处进行哪些更改以确保其构建,并且我们也没有遇到运行时错误。
答案 0 :(得分:18)
使用
private func myPathApply(_ path: CGPath!,
block: @escaping @convention(block) (UnsafePointer<CGPathElement>) -> Void) {
}
@noescape
现在默认为