[英]How do you create a formatted string like the following in Objective-C?
我是iPhone世界的新來者。 (以前,我是為Android開發的。)
我的一個Android應用程序中已包含以下代碼:
String body = "<Item type='Customer' id= '"+id+"' action='delete'/>";
Objective-C有什么相同之處?
您可以使用-[NSString stringWithFormat:]
:
NSString *body = [NSString stringWithFormat:@"<Item type='Customer' id='%@' action='delete'/>", idNum];
假設ID存儲在變量idNum
。 ( id
實際上是Objective-C中的一種類型,因此您不想將其用作變量名。)
正如Henrik所說的:
NSInteger id = 5;
NSString* body = [NSString stringWithFormat:@"<Item type='Customer' id= '%d' action='delete'/>", i];
(不過,純粹主義者可能對此表示反對。)
但是,真正的答案是通讀“ 學習Objective-C:入門” 。 它並不長,並且向您顯示了幾乎所有您需要了解的有關該語言的知識。
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.