Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Warning

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.

like image 426
IUnknown Avatar asked Dec 22 '22 09:12

IUnknown


2 Answers

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!

like image 62
Rob Avatar answered Jan 06 '23 02:01

Rob


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

NSLog(@"My home folder is %@",[path stringByExpandingTildeInPath]);
like image 37
ennuikiller Avatar answered Jan 06 '23 02:01

ennuikiller