[英]How do you create a formatted string like the following in Objective-C?
I'm a newcomer to the iPhone world. 我是iPhone世界的新来者。 (Previously, I've developed for android.)
(以前,我是为Android开发的。)
I've got this code in one of my Android apps: 我的一个Android应用程序中已包含以下代码:
String body = "<Item type='Customer' id= '"+id+"' action='delete'/>";
What's the same in Objective-C? Objective-C有什么相同之处?
You can use -[NSString stringWithFormat:]
: 您可以使用
-[NSString stringWithFormat:]
:
NSString *body = [NSString stringWithFormat:@"<Item type='Customer' id='%@' action='delete'/>", idNum];
Assuming the ID is stored in the variable idNum
. 假设ID存储在变量
idNum
。 ( id
is actually a type in Objective-C, so you don't want to use that as a variable name.) (
id
实际上是Objective-C中的一种类型,因此您不想将其用作变量名。)
As Henrik says, it's: 正如Henrik所说的:
NSInteger id = 5;
NSString* body = [NSString stringWithFormat:@"<Item type='Customer' id= '%d' action='delete'/>", i];
(A purist may argue with this, though.) (不过,纯粹主义者可能对此表示反对。)
But really the answer is to read through " Learning Objective-C: A Primer ." 但是,真正的答案是通读“ 学习Objective-C:入门” 。 It's not terribly long and shows you pretty much everything you need to know about the language.
它并不长,并且向您显示了几乎所有您需要了解的有关该语言的知识。
String body = "<Item type='Customer' id= '"+id+"' action='delete'/>";
NSString* id = @"foo";
NSString* body
= [NSString stringWithFormat:@"<Item type='Customer' id='%@' action='delete'/>", id ];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.