0

我试图将一个变量附加到一个字符串,但它显示一个错误。我在这里错过了一些非常容易的事情,但我的头脑被击中了。

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

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

3 回答 3

3

您不能仅通过在它们之间放置逗号来连接 2 个字符串变量。试试这个:

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

于 2013-10-26T18:15:23.560 回答
1

如果您所做的只是追加,您有几个选择:

A. 使用 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. 使用 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. 使用 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];
于 2013-10-26T18:21:38.610 回答
1

试试这样: -

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

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

NSURL *urla = [NSURL URLWithString:[url stringByAppendingString:eID];
于 2013-10-26T18:48:53.730 回答