Objective-C Warning

I'm new to objective-c, and it's hard for me to understand 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 get for the last line (NSLog call):

 warning: format not a string literal and no format arguments 

Can anyone clarify? Is this a warning message that I should worry about?

Thanks.

+4
source share
2 answers

Your code should work fine, but it may be incorrect if the transmitted string contains the characters "%" formatting, which may confuse NSLog. For example, try replacing this with your code:

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

NSLog interprets this "% f" in a bad way.

You can avoid the warning (and danger) by using a string literal with formatting and passing in your lines, for example:

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

You can also check this link:

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

Good luck

+9
source

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

 NSLog(@"My home folder is %@",[path stringByExpandingTildeInPath]); 
+2
source

All Articles