• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: "Format not a string literal and no format arguments"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Format not a string literal and no format arguments"


  • Subject: Re: "Format not a string literal and no format arguments"
  • From: Peter Duniho <email@hidden>
  • Date: Sat, 29 Aug 2009 11:54:32 -0700

On Aug 29, 2009, at 10:36 AM, Quincey Morris wrote:

On Aug 29, 2009, at 05:36, Jonathan del Strother wrote:

After upgrading to snow leopard & Xcode 3.2, I've starting getting
this warning on NSLogs -

NSLog(@"Hello"); // 'Format not a string literal and no format arguments'
NSLog(@"Hello %@", name); // compiles fine



I must have some weird project setting somewhere since a new test project didn't throw these warnings, but I can't see what it is. Any suggestions?

This is addressed in the Snow Leopard Foundation release notes (under NSString). This should now produce a warning:


	NSString* hello = @"Hello";
	NSLog (hello);

but this should not:

NSLog (@"Hello");

Am I reading the right documentation? On this page: http://developer.apple.com/mac/library/releasenotes/Cocoa/Foundation.html

I see this text:

    Foundation APIs which take string format arguments (APIs
    such as initWithFormat:, NSLog(), etc) are now decorated
    with attributes which allow the compiler to generate
    warnings on potentially unsafe usages. This currently
    includes usage such as:
    NSString *str = ...;
    NSString *formattedStr = [[NSString alloc] initWithFormat:str];
    where the format is not a constant and there are no arguments.
    In a case like above, the call to initWithFormat: is
    unnecessary, so to avoid warnings, just drop the call. In
    a case like NSLog(str), you can instead use NSLog(@"%@", str).

I read that to mean that one will get the warning for _any_ call to initWithFormat:, even those automatically generated by NSLog() (thus the suggestion in the text for a work-around so as to prevent the warning).

(Aside: The wording of the warning also seems weird to me, assuming Jonathan quoted it verbatim. Why should a non-literal string format be any more or less likely to include formatting specifiers that would require format arguments? Or is it implied that the compiler is doing compile-time verification of the format string when a literal is provided?)

Not having Snow Leopard, nor Xcode 3.2, I can't test this myself and I suppose I might be misunderstanding the question or the answer. But it looks to me as though any usage of initWithFormat: that doesn't pass format arguments will generate the warning, even NSLog(@"Hello").

Is there some more specific documentation of the warning available somewhere? I tried searching the docs for the warning text provided in the previous post, but didn't find anything. Searching the web using Google suggests that this is a gcc feature, intended for use where actual string literals (e.g. "Hello", not @"Hello") would appear (e.g. printf()), but based on the comments I saw in that context, it's not clear to me why this warning would be all that useful in the context of Cocoa anyway (since most "literals" are really NSString objects, not checkable by the compiler).

Perhaps due to my inexperience, I am missing something here and NSLog() really shouldn't generate the warning. If so, please feel free to set me straight. :)

Pete
_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: "Format not a string literal and no format arguments"
      • From: Ken Thomases <email@hidden>
    • Re: "Format not a string literal and no format arguments"
      • From: Quincey Morris <email@hidden>
References: 
 >"Format not a string literal and no format arguments" (From: Jonathan del Strother <email@hidden>)
 >Re: "Format not a string literal and no format arguments" (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: [OT] Re: llvm-gcc-4.2 link-time error seen from command line but not in Xcode 3.2
  • Next by Date: Re: "Format not a string literal and no format arguments"
  • Previous by thread: Re: "Format not a string literal and no format arguments"
  • Next by thread: Re: "Format not a string literal and no format arguments"
  • Index(es):
    • Date
    • Thread