views:

566

answers:

2

I am fairly new to objective-c and am having trouble understanding the warning message for the following block of code:

void PrintPathInfo() {
    NSString *path = @"~"; 
    NSString *message = @"My home folder is: ";

    NSLog([message stringByAppendingString: [path stringByExpandingTildeInPath]]);
}

This is the warning message I am getting for the last line (call to NSLog):

warning: format not a string literal and no format arguments

Can someone please clarify? Is this a warning message I should be concerned about?

Thanks.

+2  A: 

If you want to write the output to nslog you need something like this:

NSLog(@"My home folder is %@",[path stringByExpandingTildeInPath]);
ennuikiller
+7  A: 

Your code should work just fine, but could misbehave if there are any '%' formatting characters in the passed string--that could confuse NSLog. For example, try swapping this into your code:

NSString *message = @"My home %folder is: ";

NSLog will interpret that '%f' in a bad way.

You can avoid the warning (and the danger), by using a string literal with formatting and passing in your strings, like this:

NSLog(@"%@%@", message, [path stringByExpandingTildeInPath]);

You might also check this link:

http://www.cocoabuilder.com/archive/message/cocoa/2009/8/29/243819

Good luck!

Rob
Thanks - that makes sense now.
IUnknown

related questions