I have mentioned NSString in previous posts, so I might as well write about it in my new set of class reference posts. I also keep forgetting the formatting specifiers, and so I’ll put up a table of those for everyone to see when you want to know the correct specifier for a formatted string. As we’ve seen before, an NSString is an object that represents a block of text. The name presumably comes from the fact that it is a string of characters, though that is just supposition, but does make it slightly easier to remember. For a little history, in C, a string was an array of an 8-bit variable named “char”, which was short for character. In each of those spots of an array was a number that would denote a letter based off of the ASCII standard. If you’re curious, here is a link to a copy of the ASCII Table. At the end of the string, to denote the end, was a NUL character, which is denoted by the number 0. Unlike most of my trips into history though, this is actually still quite relevant to Objective-C.
Creating Strings
The simplest was to create an NSString is be creating a string literal, like so:
NSString *myFancyString = @"Hello World! I am a string!"
Why is it called a string literal? Because it is the string literally written in the code. I know, it’s a bit simplistic, but it is a term that is used for them. Most useful string are generated programaticcaly, as I will show next, so it does have some significance.
So for this string, one important thing to note is that @ sign before the quotes. Without it, you would actually be creating a standard C, null-terminated string. X-Code then complains “Implicit conversion of a non-Objective-C pointer type ‘char *’ to ‘NSString *’ is disallowed with ARC.” When you add that @ sign, it commands the compiler to create it as an NSString. The @ sign is used in a few places in for shorthand like this, such as in NSSArray, where it can help with making arrays. Other that, nothing is too special about this line, an NSString literal must start with a @ sign, and and be enclosed in double quotes. Single quotes normally denotes a single character in C and apparently gives warnings instead of syntax errors, but definitely do not do what you would want with the string. Single quote can be used for single characters, but that’s another story.
To generate a string programatically using variable values, you would do something similar to the code below:
NSString *somethingMissing = @"cupcakes";
int amountLeft = 5;
NSString *someSentence = [NSString stringWithFormat:@"We seem to have only %d of our %@ left!", amountLeft, somethingMissing];
So, basically, you send the NSString class a message requesting that it use the string you send it, and replace some placeholders in it with data stored in variables. The “%d” and “%@” are placeholders for certain types of variables. After the main string you see some variables separated by commas. These fill in those placeholders in the same order that the placeholders are written, so amountLeft left’s value will replace the %d, and somethingMissing will replace the %@. Which identifier goes with what? That is what I mentioned at the beginning that I would put a table in for. For the full table, see that String Programming Guide: String Format Specifiers, but here are some excerpts that I will probably use the most often.
Identifier | What it replaces it |
---|---|
%@ | An Objective-C Object. It calls the method “description” which outputs a string representation of whatever value it is. |
%d or %D | A Signed 32-bit integer (int) |
%u or %U | An unsigned 32-bit integer (unsigned int) |
%f | A 64-bit floating-point number (double), be default to 6 decimal places, so 5.341847924 comes out as 5.341848. |
Are these NSStrings equal?
How can you find the answer to that question in Objective-C? It would be nice if you could say if( someString == anotherString), but that would only try to equate their pointers. In this case you have to ask NSString to do it for you, with:
NSString *someCat = @"Orange Tabby";
if ([someCat isEqualToString:@"Orange Tabby"])
{
NSLog(@"It's an Orange Tabby!");
}
if ([someCat isEqualToString:@"Corgi"])
{
NSLog(@"It's a Corgi!");
}
Here we are asking the someCat instance of NSString if it is equal to another string. This message responds with a boolean ‘YES’ or ‘NO’, which will inform the if statement what to do. In this case it will print out “It’s an Orange Tabby!”, but will not print “It’s a Corgi!”.
That’s it for now. I should be updating this more in the future with some other useful abilities of the NSString class. I hope this has been a help to you, and good luck in learning Objective-C.