On Aug 29, 2009, at 11:54, Peter Duniho wrote:

Or is it implied that the compiler is doing compile-time verification of the format string when a literal is provided?

Sort of, but not exactly. The first parameter to NSLog is *really* a format string, not just a string. If an arbitrary string passed as the first parameter happens to contain a stray '%' character, that's likely going to result in a crash.

So if you provide a string literal, there's no warning because it's assumed you're not going to hard-code stray '%' characters in it. If you provide arguments after the string (whether literal or not), it's assumed you've made sure the string really is a format string. If you provide a non-ilteral string and no arguments, there's a fair chance that you've forgotten that some potential string values will blow up, hence the warning.

The compiler's reasoning is heuristic, not impeccable. You can certainly do the wrong thing using a string literal too (or by giving the wrong number of arguments, for that matter), but the warning is generated in a commonly-overlooked case, not in all potentially buggy cases.

In Jonathan's case, the string *looked* like a literal in the source code, but wasn't, because of his compile options. Presumably his non- literal string is never going to get changed to something bad, but he'll have to code around it to keep the compiler from complaining.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to