如何在Objective-C中使用stringWithFormat方法追加字符串变量

Mad*_*han 7 append objective-c stringwithformat

我想使用stringWithFormat将字符串附加到单个varilable中.我在使用stringByAppendingString时知道它.请帮我使用stringWithFormat为下面的代码添加.

NSString* curl = @"https://invoices?ticket=";
curl = [curl stringByAppendingString:self.ticket];
curl = [curl stringByAppendingString:@"&apikey=bfc9c6ddeea9d75345cd"];
curl = [curl stringByReplacingOccurrencesOfString:@"\n" withString:@""];
Run Code Online (Sandbox Code Playgroud)

谢谢,Madan Mohan.

Vla*_*mir 14

您可以curl使用-stringWithFormat:方法构造字符串:

NSString *apiKey = @"bfc9c6ddeea9d75345cd";
NSString* curl = [NSString stringWithFormat:@"https://invoices?ticket=%@&apikey=%@", self.ticket, apiKey];
Run Code Online (Sandbox Code Playgroud)

  • 为了对此进行扩展,要将对象附加到字符串,使用%@,以便在上面的示例中,[self.ticket description]和[apiKey description]将被替换.传递给-description是隐式的.这也可以让你使用相同的语法传递NSNumber之类的东西.如果要传递除objc对象之外的任何其他数据类型,则支持与printf()和friends相同的修饰语法; 请参阅各自的手册页,了解每个选项的功能. (3认同)