Dangerous Logging in Swift
indiestack.com
indiestack.com
However the intent of the Swift call described in the article is indeed that of a string literal with 2 data "parameters".
So is the correct use of NSLog in this case is this:
NSLog("%@", "Failed to get modification date at URL: \(url) - \(error)")
This should really be the behavior of NSLog in Swift, that it expands not to the variadic NSLog(format-string-parameter, objc-object-parameters..)
but to NSLog(@"%@", string-parameter)NSLog(“Failed to get modification date at URL: %@ - %@“, url.absoluteString, error.localizedDescription)
So a native Swift API would be able to distinguish and refuse to let you provide this String where a StaticString is the appropriate thing, I am not an Apple expert to say whether this would be possible / easy for the NSLog binding, if it was possible to do this it should have been done.
NSLog might be considered an exception vs other Objective-C usage of format strings - but it might also not be worth having a singular special case.
I'm surprised here that the author ends up believing that, on the one hand, this function behaves as if it has variadic parameters (hence the format specifiers), but then on the other hand they have no option but to somehow write all their text as the first parameter. These are logically inconsistent beliefs, but maybe too much fighting with Apple's ecosystem causes you to just stop believing the world has to make sense.
Because of this and the author's issue, I don't see why you should ever use NSLog in Swift. Even if you need a timestamp, you could just write a custom logging function using printf.
I always use print(), which has its own issues.
Of course just because the constraint is known doesn't mean (as you illustrate) that everybody knows about it or makes use of it in their own programming.
Imagine that, expecting a RESTful API for some new remote service you're accessing you discover it instead offers precise instructions for where the Javascript buttons are rendered on a 640x480 Internet Explorer 6 browser, instructing you to automate pressing the buttons through a browser automation gadget.
You'd be aghast right? Sure this would work but it's clearly not an ergonomic way to provide automation, why not just offer a RESTful API like most similar sites?
Dynamically constructing format strings is the same ergonomic awkwardness, for the benefit of lazy workers who couldn't be bothered to actually deliver what was needed.
Instead I want a way to reach into the same formatting infrastructure that is used for my string literal formats, and manipulate it dynamically, not by creating "format strings" dynamically.
As an example, if your formatting library can format("{some}{values}{with:parameters}", v1, v2, v3) I ought to be able to write my own different function myfn("[different][syntax][same#features]", v1, v2, v3) re-using the infrastructure from the existing formatting library, rather than needing to write a function myfn("[different][syntax][same#features]") which has a result "{some}{values}{with:parameters}" in order to deliver the same effect but via this sub-language.