Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke method with CVaListPointer parameters in Swift

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?

like image 493
mikywan Avatar asked Apr 27 '16 18:04

mikywan


1 Answers

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.

like image 57
Martin R Avatar answered Oct 17 '22 05:10

Martin R