Talktobijju
Talktobijju

Reputation: 450

How do you create a formatted string like the following in Objective-C?

I'm a newcomer to the iPhone world. (Previously, I've developed for android.)

I've got this code in one of my Android apps:

String body = "<Item type='Customer' id= '"+id+"'  action='delete'/>";

What's the same in Objective-C?

Upvotes: 1

Views: 1569

Answers (3)

AndersK
AndersK

Reputation: 36092

String body = "<Item type='Customer' id= '"+id+"'  action='delete'/>";


NSString* id = @"foo";
NSString* body 
  = [NSString stringWithFormat:@"<Item type='Customer' id='%@' action='delete'/>", id ];

Upvotes: 1

Stephen Darlington
Stephen Darlington

Reputation: 52575

As Henrik says, it's:

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." It's not terribly long and shows you pretty much everything you need to know about the language.

Upvotes: 1

mipadi
mipadi

Reputation: 411252

You can use -[NSString stringWithFormat:]:

NSString *body = [NSString stringWithFormat:@"<Item type='Customer' id='%@' action='delete'/>", idNum];

Assuming the ID is stored in the variable idNum. (id is actually a type in Objective-C, so you don't want to use that as a variable name.)

Upvotes: 6

Related Questions