我正在使用
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的答案改变它允许它编译,但它得到一个运行时错误:
“致命错误:不能在不同大小的类型之间使用unsafeBitCast”
上面的示例中提供了一条评论,我没有在上面的示例中包含:
// Note: You must declare MyPathApplier as @convention(block),on Mac OS X at least.
我仍然不明白需要在这里改变什么以确保它构建,我们也没有得到运行时错误.
解决方法
使用
private func myPathApply(_ path:CGPath !,block:@escaping @convention(block)(UnsafePointer< CGPathElement>) – > Void){
private func myPathApply(_ path:CGPath !,block:@escaping @convention(block)(UnsafePointer< CGPathElement>) – > Void){
@noescape现在默认为