user520300
user520300

Reputation: 1527

How to append a variable to a string in xcode

Im trying to to append a variable to a string but its showing an error. Im missing something ridiculously easy here but my mind is shot.

NSString *eID = [entertainmentArticle objectForKey:@"eID"];

NSURL *urla = [NSURL URLWithString:@"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=",eID];

Upvotes: 0

Views: 649

Answers (3)

Hussain Shabbir
Hussain Shabbir

Reputation: 14995

Try like this:-

NSString *eID = [entertainmentArticle objectForKey:@"eID"];

 NSString *url=@"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";

NSURL *urla = [NSURL URLWithString:[url stringByAppendingString:eID];

Upvotes: 1

Senseful
Senseful

Reputation: 91641

If all you are doing is appending, you have several options:

A. Use NSString & stringWithFormat:

NSString *eID = [entertainmentArticle objectForKey:@"eID"];
NSString *urlString = [NSString stringWithFormat:@"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%@", eID];
NSURL *urla = [NSURL URLWithString:urlString];

B. Use NSString & stringByAppendingString:

NSString *eID = [entertainmentArticle objectForKey:@"eID"];
NSString *baseUrl = @"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSString *urlString = [baseUrl stringByAppendingString:eID];
NSURL *urla = [NSURL URLWithString:urlString];

C. Use NSMutableString & appendString:

NSString *eID = [entertainmentArticle objectForKey:@"eID"];    
NSString *baseUrl = @"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSMutableString *urlString = [NSMutableString stringWithString:baseUrl];
[urlString appendString:eID];
NSURL *urla = [NSURL URLWithString:urlString];

Upvotes: 1

RyanR
RyanR

Reputation: 7758

You can't concat 2 string variables just by putting a comma between them. Try this instead:

NSURL *urla = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%@",eID]];

Upvotes: 3

Related Questions