views:

52

answers:

2

I am a java programer, I found that the Java is very gd at doing string. If I want to do this objective c, how can I do in objective c:

System.out.println("This is a "+123+" test");
+5  A: 

To place an integer into a string, you can do this:

int n = 123;
NSString *s = [NSString stringWithFormat:@"This is a %d test", n];

There are numerous other ways. But concatenating strings with integers by + operator is not one of them. :)

Seva Alekseyev
And for printing debugging output to the console, NSLog() is your friend.
David Gelhar
As in, NSLog(CFSTR("This is a %d test"), n); The difference of syntax has to do with the fact that NSString is an Objective C class with methods, and NSLog is a C function.
Seva Alekseyev
@Seva that works, but it's much easier to do: `NSLog(@"This is a %d test", n);` Even though `NSLog` is a C function, it takes `NSString*,...` as its arguments.
Dave DeLong
You know... conceptual purity :) I'm pretending CFString and NSString are distinct, even though they're the same.
Seva Alekseyev
But you can have C functions that take objective C objects that don't have Core Foundation equivalents…
Wevah
Well, yeah... so what? NSLog is documented to take a CFStringRef as a parameter. So I gave it one.
Seva Alekseyev
A: 

To simply print to the console you could try:

int num = 123;
printf("This is a %i test", num);
Leom Burke
On iPhone, you have to print to stderr IIRC.
Seva Alekseyev
NSLog is more idiomatic on Mac/cocoa than printf.
Warren P
Thats cool - I was being too general I guess :)
Leom Burke