'NSLog' is unavailable: Variadic function is unavailable in swift

Have the same problem, accepted answer doesn't help.

func handleSomeResp(_ response: DataResponse<Any>) {
    NSLog("handle error: %@", response)
}

as you see, there is nothing about optionals. I guess it because of <Any>, and any type, unclear for NSLog, watch similar problem here: . Edit this answer if you have a solution to parse generics for NSLog.


NSLog() cannot print Swift Optionals.

let optional: String?
NSLog("%@", optional) // 'NSLog' is unavailable: Variadic function is unavailable

let nonOptional: String
NSLog("%@", nonOptional) // Ok!

NSLog("%@", optional ?? "value-if-nil") // Ok!

Fix by passing a non-optional value to NSLog() instead.

NOTE:

print() can print Swift Optionals.


If you declare var myString: NSString? as an optional then you need to make sure it has a value before you pass it to the NSLog.

So you could do it like this then NSLog("%@" , myString!). If myString is nil and you put ! the program will crash and you will get

fatal error: unexpectedly found nil while unwrapping an Optional value.

But if it has a value the program will continue as normal and print out

2016-10-03 10:26:25.077 Swift3.0[65214:1579363] Test

I wrote myString = "Test".

Another example as @Grimxn mentioned is the coalescing operator.

NSLog("%@", myString ?? "<nil>")

Tags:

Ios

Nslog

Swift