How to invoke method with CVaListPointer parameters in Swift

不打扰是莪最后的温柔 提交于 2019-12-08 16:18:20

问题


How should I invoke the following method? The method belongs to a class that prints logs.

func log(format: String!, withParameters valist: CVaListPointer)

What I want to achieve, would look like this in Objective-C:

NSLog(@"Message %@ - %@", param1, param2);

Any ideas?


回答1:


CVaListPointer is the Swift equivalent of the C va_list type and can be created from an [CVarArgType] array using withVaList().

Example:

func log(format: String!, withParameters valist: CVaListPointer) {
    NSLogv(format, valist)
}

let args: [CVarArgType] = [ "foo", 12, 34.56 ] 
withVaList(args) { log("%@ %ld %f", withParameters: $0) }

Output:

2016-04-27 21:02:54.364 prog[6125:2476685] foo 12 34.560000

For Swift 3, replace CVarArgType by CVarArg.



来源:https://stackoverflow.com/questions/36898427/how-to-invoke-method-with-cvalistpointer-parameters-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!