views:

57

answers:

3

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?

+1  A: 

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.

Stephen Darlington
I prefer Henrik's version because the pointer sign is near the variable, which is much clearer when having something like `int value, *pointer;` On the other hand, I agree with you that he needs to learn Objective-C by reading a book.
Georg
I changed the answer to reflect the fact that the question was altered.
Stephen Darlington
@Georg, this is something I change my opinion on every couple of years! This years take is that the pointery-ness of a variable is part of the type. I tend to list variables declarations on separate lines anyway.
Stephen Darlington
+3  A: 

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.)

mipadi
Nor do you want %@ as a format specifier for something that's probably a C integer type. It's OK if idNum is an NSNumber.
JeremyP
A: 
String body = "<Item type='Customer' id= '"+id+"'  action='delete'/>";


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