You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-9109 String.init(format::), String.init(format:arguments:), String.init(format:locale::), String.init(format:locale:arguments), and String.localizedStringWithFormat are dangerous and should be removed.
A version of CFStringCreateWithFormat which takes a number of arguments isn’t any better than what we have today — between the format string and the arguments themselves, the source of truth is taken to be the format string. Because of how C is, there’s no way to know how many arguments are passed in, so there’s no way to validate the input against the source of truth
Adding another parameter adds further potential inconsistency with that source of truth:
You can mistype the count but have the correct format string and number of arguments
You can have the right count and format string, but still pass in the wrong number of arguments
You can have the right number of arguments and count, but screw up the format string
Rinse and repeat for all combinations, including the count, format string, and arguments being inconsistent
Instead, we have the compiler to verify things in C and Obj-C for CFStringCreateWithFormat and -[NSString initWithFormat:] — both are marked with the same attributes as the printf family of functions so the compiler warns on mismatches and inconsistencies, as it should:
CFStringRef string = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d, %d"), 5); // warning: more '%' conversions than data arguments
If a developer chooses to ignore those warnings, then it’s no surprise if things don’t work.
What we don’t have, though, is a similar facility in Swift:
let str = String(format: "%d, %d", 5)
print(str)
produces no warnings. It would be nice to be able to annotate String.init(format:_🙂 with a similar annotation to keep developers safe in the same way.
If we want to maintain the use of CVarArg in swift the compiler should help with the format validation implementing something like the objc `NS_FORMAT_FUNCTION()`
A real-life example is os_log, that is the way-to-go for logging but it's a pain to use, with countless crashes due to wrong format strings.
Something like this:
os_log("ups %@, %@, %d", "bug!")
In swift compiles with no problems, when in Objc gives you the proper warnings.
The text was updated successfully, but these errors were encountered:
Additional Detail from JIRA
md5: cd16cac8882354c1bbbf9f0719d8c2dd
relates to:
Issue Description:
From: https://bugs.swift.org/browse/SR-9109
Quoting @itaiferber:
If we want to maintain the use of CVarArg in swift the compiler should help with the format validation implementing something like the objc `NS_FORMAT_FUNCTION()`
A real-life example is os_log, that is the way-to-go for logging but it's a pain to use, with countless crashes due to wrong format strings.
Something like this:
In swift compiles with no problems, when in Objc gives you the proper warnings.
The text was updated successfully, but these errors were encountered: