func swiftprintf(format: String, _ arguments: CVarArg...) -> String? {
return withVaList(arguments) { va_list in
var buffer: UnsafeMutablePointer<Int8>? = nil
return format.withCString { CString in
guard vasprintf(&buffer, CString, va_list) != 0 else {
return nil
}
return String(validatingUTF8: buffer!)
}
}
}
swiftprintf(format: "√2 ≅ %g", sqrt(2.0))
输出结果:√2 ≅ 1.41421