任何人都可以解释为什么这个带有隐式return
的单行块编译:
let r = withUnsafePointer(&msg) {
dn_expand(UnsafePointer($0), eomorig: UnsafePointer($0).advancedBy(msg.count), comp_dn: UnsafePointer($0).advancedBy(offset), exp_dn: &buf, length: buf.count)
}
但是这个版本重构了唯一的区别是避免多次调用UnsafePointer($0)
不会:
let s = withUnsafePointer(&msg) {
let p = UnsafePointer($0)
return dn_expand(p, eomorig: p.advancedBy(msg.count), comp_dn: p.advancedBy(offset), exp_dn: &buf, length: buf.count)
}
错误消息:
Cannot convert value of type 'inout [UInt8]' (aka 'inout Array<UInt8>') to expected argument type 'inout _'
被调用的dn_function
只是来自dn_expand
的{{1}}的一个微不足道的包装:
libresolv
答案 0 :(得分:2)
正如评论中已经说过的那样,withUnsafePointer()
不是
获取指向元素存储的指针的正确方法。它编译,但是
给出了意想不到的结果,如下例所示:
var msg: [UInt8] = [1, 2, 3, 4]
func foo(x: UnsafePointer<UInt8>) {
print(x[0])
}
withUnsafePointer(&msg) {
foo(UnsafePointer($0))
}
这打印&#34;随机&#34;数字,但不是预期的1
。正确的
方法是调用数组上的withUnsafeBufferPointer()
方法:
msg.withUnsafeBufferPointer {
foo($0.baseAddress)
}
在你的情况下
let r = msg.withUnsafeBufferPointer {
dn_expand($0.baseAddress, eomorig: $0.baseAddress + $0.count, ...)
}
这里自动推断闭包的返回类型,因为 它是一个单一的表达方式&#34;关闭。如果封闭包含更多 如果不是一个表达式,则必须指定其类型:
let r = msg.withUnsafeBufferPointer { bufPtr -> Int in
let p = bufPtr.baseAddress
return dn_expand(p, eomorig: p + msg.count, ...)
}
或让编译器从上下文推断返回类型:
let r: Int = msg.withUnsafeBufferPointer { bufPtr in
let p = bufPtr.baseAddress
return dn_expand(p, eomorig: p + msg.count, ...)
}