swiftnslog

NSLog is unavailable


I have following function:

func myNSLog(_ givenFormat: String, _ args: CVarArg..., _ function:String = #function) {
        let format = "\(function): \(givenFormat)"
        NSLog(format, args)

Which results in the following error:

'NSLog' has been explicitly marked unavailable here (Foundation.NSLog)

Within the documentation is it explicit listed as available. What do I miss?


Solution

  • Similar as in C, you cannot pass a variable argument list directly to another function. You have to create a CVaListPointer (the Swift equivalent of va_list) and pass that to the NSLogv variant:

    func myNSLog(_ givenFormat: String, _ args: CVarArg..., _ function:String = #function) {
        let format = "\(function): \(givenFormat)"
        withVaList(args) { NSLogv(format, $0) }
    }
    

    (Swift 3 code.)