2016-04-27 10 views
5

Come si deve richiamare il seguente metodo? Il metodo appartiene a una classe che stampa i registri.Come richiamare il metodo con i parametri CVaListPointer in Swift

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

Quello che voglio raggiungere, sarebbe simile a questa in Objective-C:

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

Tutte le idee?

risposta

10

CVaListPointer è l'equivalente Swift del C va_list tipo e può essere creato da un [CVarArgType] array utilizzando withVaList().

Esempio:

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

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

uscita:

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

Per Swift 3, sostituire CVarArgType da CVarArg.

+0

Che cosa puoi raccomandare per una funzione che richiede 'CVarArg' invece di' CVaListPointer'? – kelin

+0

Se sostituisci 'CVarArgType' di' CVarArg', il compilatore mostrerà un errore a causa del tipo, e se proverai a lanciare 'CVarArg' in' CVarArgType', si verificherà un arresto anomalo. – kelin

Problemi correlati