Kann mir jemand erklären, warum diese einzelne Zeile Block mit einem impliziten return
kompiliert:Swift Block Syntax Versagen abzuleiten Typ
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)
}
aber diese Version Refactoring, wo der einzige Unterschied die mehrere Anrufe zu UnsafePointer($0)
zu vermeiden, ist nicht :
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)
}
mit Fehlermeldung:
Cannot convert value of type 'inout [UInt8]' (aka 'inout Array<UInt8>') to expected argument type 'inout _'
Die dn_function
genannt wird, ist nur eine triviale Wrapper um dn_expand
von libresolv
:
public static func dn_expand(msg: UnsafePointer<UInt8>, eomorig: UnsafePointer<UInt8>, comp_dn: UnsafePointer<UInt8>, exp_dn: UnsafeMutablePointer<CChar>, length: Int) -> Int {
return Int(res_9_dn_expand(msg, eomorig, comp_dn, exp_dn, Int32(length)))
}
Was die Signatur für ' dn_expand'? –
Der Compiler leitet den Typ eines Abschlusses automatisch nur für * single-expression-closures * oder aus dem * context * ab, vergleiche http://stackoverflow.com/a/34115788/1187415. –
@MartinR Ich vermutete als solche, aber ich kann nicht den richtigen expliziten Typ selbst bestimmen – Alnitak