user567
user567

Reputation: 3842

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 error:

too many argument to method call, expected ...

Please help.

Upvotes: 0

Views: 2637

Answers (3)

jscs
jscs

Reputation: 64002

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.

Upvotes: 2

ryanprayogo
ryanprayogo

Reputation: 11817

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.

Upvotes: 0

Stunner
Stunner

Reputation: 12194

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

Hope that helps.

Upvotes: 0

Related Questions