开发者

Putting the value of a text field into another string

When I write this:

NSLog("Text Value %@",statutsField.text);

It work fine , but when I do this:

NSURL *url = [NSURL URLWithString:@"http://MyUrl/%@",statutsField.text];

I get an开发者_JAVA百科 error:

too many argument to method call, expected ...

Please help.


URLWithString: only accepts one argument; one single NSString. You are passing it two, the string @"http://MyUrl/%@" and the string in statutsField.text.

You need to construct a combined version of the string, and pass that combined version to URLWithString:. Use +[NSString stringWithFormat:] for this:

NSString * myURLString = [NSString stringWithFormat:@"http://MyUrl/%@", statutsField.text]
NSURL * myURL = [NSURL URLWithString:myURLString];

The function NSLog accepts a variable number of arguments, based on the number of format specifiers that it finds in its first string (the format string); this is why your NSLog call works. The method stringWithFormat: works similarly. For each %@ it finds in its first argument, it takes an object from the rest of the argument list and puts it into the resulting string.

For details, you can see Formatting String Objects in the String Programming Guide.


Try [NSURL URLWithString:[NSString stringWithFormat:@"http://MyUrl/%@",statutsField.text]];

Hope that helps.


Try this:

NSString *base = @"http://MyUrl/";
NSString *urlString = [base stringByAppendingString:statutsField.text];

NSURL *url = [NSURL URLWithString:urlString];

The method URLWithString accepts only 1 argument, but you are passing 2 arguments, ie, the string @"http://MyUrl/%@" and statutsField.text

So you have to concatenate the string beforehand, or use the stringWithFormat method of NSString inline.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜