0

I used this code from the Stack Overflow question: URLWithString: returns nil:

//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

When I copied it into my code, there wasn't any issue but when I modified it to use my url, I got this issue:

Data argument not used by format string.

But it works fine. In my project:

.h:

NSString *localisationName;

.m:

NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* stringURL = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Hősök_tere", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

[_webView loadRequest:[NSURLRequest requestWithURL:url]];

How can I solve this? Anything missing from my code?

Community
  • 1
  • 1
Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42

2 Answers2

1

The @ in the original string is used as a placeholder where the value of webName is inserted. In your code, you have no such placeholder, so you are telling it to put webName into your string, but you aren't saying where.

If you don't want to insert webName into the string, then half your code is redundant. All you need is:

NSString* stringURL = @"http://en.wikipedia.org/wiki/Hősök_tere";
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

[_webView loadRequest:[NSURLRequest requestWithURL:url]];
Jim
  • 72,985
  • 14
  • 101
  • 108
0

The +stringWithFormat: method will return a string created by using a given format string as a template into which the remaining argument values are substituted. And in the first code block, %@ will be replaced by value of webName.

In your modified version, the format parameter, which is @"http://en.wikipedia.org/wiki/Hősök_tere", does not contain any format specifiers, so

NSString* stringURL = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Hősök_tere", webName];

just runs like this (with the warning Data argument not used by format string.):

NSString* stringURL = @"http://en.wikipedia.org/wiki/Hősök_tere";

Hvordan har du det
  • 3,605
  • 2
  • 25
  • 48