views:

313

answers:

5

Hi, I'm working through the Stanford iPhone podcasts and have some basic questions.

The first: why is there no easy string concatenation? (or am I just missing it?)

I needed help with the NSLog below, and have no idea what it's currently doing (the %@ part). Do you just substitute those in wherever you need concatenation, and then comma separate the values at the end?

NSString *path = @"~";
NSString *absolutePath = [path stringByExpandingTildeInPath];

NSLog(@"My home folder is at '%@'", absolutePath);

whereas with any other programing language I'd have done it like this:

NSLog(@"My home folder is at " + absolutePath);

Thanks! (Additionally, any good guides/references for someone familiar with Java/C#/etc style syntax transitioning to Objective-C?)

+2  A: 

That is a string format specifier. Basically it allows you to specify a placeholder in the string and the values that are to be inserted into the placeholder's spot. The link I reference above lists the different notations for the placeholders and each placeholder's specific format.

It's just like C#'s String.Format method:

NSLog(String.Format("My home folder is at '{0}'", absolutePath));
Andrew Hare
+5  A: 

%@ is a placeholder in a format string, for a NSString instance.

When you do something like:

NSLog(@"My home folder is at '%@'", absolutePath);

You are saying NSLog to replace %@ placeholder with absolutePath string value.

Likewise, if you put more placeholders, you can specify more values to replace those placeholders like this:

NSString *absolutePath = @"/home/whatever";
NSLog(@"My home #%d folder is at '%@'", 5, absolutePath);

Will print:

My home #5 is at /home/whatever

An easy way to do string concatenation:

NSString *s1 = @"Hello, ";
NSString *s2 = @"world.";
NSString *s = [NSString stringWithFormat:@"%@%@", s1, s2];
// s will be "Hello, world."

You can't have + sign as a string concatenate operator, since there is no operation overloading in Objective-C.

Hope it helps.

Pablo Santa Cruz
Cool, thanks for the clear and simple explanation. For my example, would it be better ("more objective-C-like") to do it via a placeholder or via string concatenation?
cksubs
String format specifiers can print other types apart from Objective-C objects, such as integers (`%i`), doubles (`%f`), C strings (`%s`) and so on. You can't do this with the concatenation operators. String format specifiers come from C, and are used in functions like `printf()` for output. The `%@` syntax is an Objective-C addition to the existing C string format specifiers which prints the output of the `-description` method of an Objective-C object.
Rob Keniger
I would personally use placeholders. But it is not wrong if you do it with string concatenation... :-)
Pablo Santa Cruz
+2  A: 

You can use NSString +stringWithFormat to do concatenation:

NSString* a = // ...
NSString* b = // ...
NSString* a_concatenated_with_b = [NSString stringWithFormat:@"%@%@",a,b];

The reason for the "%@" is that the string formatting is based off of and extends the printf format strings syntax. These functions take a variable number of arguments, and anything beginning with a percent sign (%) is interpreted as a place holder. The subsequent characters determine the type of the place holder. The standard printf does not use "%@", and since "@" is the symbol commonly used for things that Objective-C adds to the C language, it makes sense that the "@" would symbolize "an Objective-C object".

There is no automatic concatentation using the plus sign (+), because NSString* is a pointer type, and Objective-C is a strict superset of C, and so, consequently, adding to an NSString* object does pointer manipulation. Objective-C does not have any operator overloading feature as in the C++ language.

Michael Aaron Safyan
A: 

The answers above give the "how"... as to the "why" you can't simply add two strings together to concatenate them, on a fundamental level I'd say it's because Objective-C is a throwback language from the 1980s, and a rather crude one at that. It only remains in use because Steve Jobs insists on it, and he's big enough to do that.

Joe Strout
Please refrain from starting language wars. It doesn't help anyone, least of all the original poster.
Rob Keniger
Wow. Did you register just to post that? Awesome.
mmc
No, I was already registered. But I'll admit that this wasn't the most useful response I've posted so far. My apologies for that -- I wasn't meaning to start a language war (I'm an ObjC user myself), but merely trying to answer the "why" part of the question at the level I thought the poster was asking for.
Joe Strout
A: 

Also, %@ is fairly versatile, as it actually inserts the result of the argument's description method into the result string. For NSString, that's the string's value, other classes can provide useful overrides. Similar to toString in Java, for example.

Peter