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?)
The basic syntax of the C program consists of header, main() function, variable declaration, body, and return type of the program. The header is the first line in the C program with extension . h which contains macro definitions and C functions.
Objective-C is the programming language that is used to write applications for Apple's iOS and OS X operating systems. The Objective-C programming language is based on C, but it adds support for object-oriented programming. All Objective-C programming is done with the Foundation framework.
Objective-C uses the same phraseology as the C language. Like in C, each line of Objective-C code must end with a semicolon. Blocks of code are written within a set of curly brackets. All basic types are the same, such as int, float, double, long and more.
Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.
%@
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 telling NSLog to replace the %@
placeholder with the string called absolutePath.
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 a +
sign as a string concatenate operator, since there is no operator overloading in Objective-C.
Hope it helps.
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With