Crash in NSLog when used in swift – and how to avoid it

As a senior iOS programmer, I have learned to use and love a logging feature method called NSLog(). It is usually called with a format string and some arguments. If I have a function callback that comes with two parameters called response and error and I want to print them out, I can do that in Objective-C with the following line:

NSLog(@”response %@, error %@”, response, error);

In that line, the pattern %@ is replaced by the arguments added behind the format string. This also means, it is not so easy to print the pattern %@, because that has a special meaning. But back to my todays problem.

In swift, the same method is very similar:

NSLog(“response %@, error %@”, response, error)

But due to a nice feature in the string class of swift which allows it to include variables in strings, if they are surrounded by backspace and parenthesis, e.g. “\(variable)”, I was writing the above line in the following way:

NSLog(“response \(response), error \(error)”)

It always worked – until today – where it crashed. Why? Because the first string given to NSLog is the format string. This means, that if the content of \(response) creates a special meaning string, e.g. %@ then NSLog tries to interpret that according to the rules. So one safe way to print the same line is, by giving a format string:

NSLog(“%@”, “response \(response), error \(error)”)

This prevents the real string to be interpreted as a format string, because now it is an argument to the format string.

Of course, I had a distant memory, that I had made a similar error when using printf in my first year(s) as a C programmer. By changing to swift, I just had forgotten about checking for this side effect, that I am aware now for so long. A nice example of risk management and how to forget about the risks of a situation when you change the environment.