Home > Articles > Programming > Mac and iOS Programming

  • Print
  • + Share This
This chapter is from the book

Using Strings

Next to numbers, the other type of data that you will spend a large amount of time with is strings. Strings may just be pieces of text, but they are used extensively throughout every kind of application. Knowing how to use them efficiently and properly is essential to developing applications for the Mac or any other platform.

In the preceding section, you were actually using strings. Every time you wrote code that used the NSLog function, you were using strings. The @ operator in Objective-C is a convenient shortcut for the creation of a string. In addition to being a convenient shortcut, it also provides a way of distinguishing between a classic C-style string and a true Objective-C string object. Strings in Objective-C are objects, not simple values. The difference between the two will become clear in the next section on pointers and equality.

There are two different kinds of strings in Objective-C: NSString and NSMutableString. As their names imply, the former represents an instance of an immutable string, whereas the latter represents an in-memory string that can be manipulated. Many languages, including C# and Java, enforce that all strings are immutable and that anytime you “modify” a string, you are actually creating a new copy containing the modification.

Creating and Manipulating Strings

Now that you’ve had a brief introduction to strings, let’s write some code to experiment with them. Follow these steps to create the sample code:

  1. Add the following lines of code to the sample you’ve been working on:
    NSString *favoriteColor = @"green";
    NSLog(@"%@", favoriteColor);
    NSString *friendlyOutput =
       [NSString stringWithFormat:
          @"Kevin loves the color %@", favoriteColor];
    NSLog(@"%@", friendlyOutput);
    NSString *subString = [friendlyOutput
      substringWithRange:NSMakeRange(6, 15)];
    NSLog(@"substring: %@", subString);

There’s a lot going on here that you may not be familiar with, so let’s walk through it line by line.

The first line creates a new variable called favoriteColor, which is a pointer to an object of type NSString (again, don’t worry about pointers just yet, we’re building up to them slowly but surely).

The second line uses the %@ string format character to log the string object. Just like standard C format specifiers such as %d and %f are used to represent numbers, the %@ format string in Objective-C is used to represent an object. All objects in Objective-C know how to describe themselves, and whenever an object is used as a parameter to the %@ format specifier, that object is asked to describe itself. Conveniently, the way a string describes itself is by returning its contents.

The third line creates a new string using a format string. Don’t worry if the square bracket syntax looks odd; we’ll get to an explanation of those in the section on message passing. Until then, try to mentally replace the square brackets with the idea that a message is being sent to an object.

Next we create a new string by extracting a substring using a range of indices. In the preceding code, we’re extracting by starting at string index 6 (strings are indexed starting at 0, just like C arrays) and pulling the following 15 characters.

  1. Run the sample to see what kind of output you get. It should now look something like this:
    2011-05-29 13:10:03.351 Hour3[1076:903] a*b = 50
    2011-05-29 13:10:03.354 Hour3[1076:903] d*e = 28905.000000
    2011-05-29 13:10:03.356 Hour3[1076:903] d/e = 0.523404
    2011-05-29 13:10:03.358 Hour3[1076:903] d/0 = inf
    2011-05-29 13:10:03.359 Hour3[1076:903] size of float: 4
    2011-05-29 13:10:03.360 Hour3[1076:903] size of double: 8
    2011-05-29 13:10:03.360 Hour3[1076:903] size of int: 4
    2011-05-29 13:10:03.361 Hour3[1076:903] size of long: 8
    2011-05-29 13:10:03.363 Hour3[1076:903] size of uint: 4
    2011-05-29 13:10:03.366 Hour3[1076:903] green
    2011-05-29 13:10:03.367 Hour3[1076:903] Kevin loves the color green
    2011-05-29 13:10:03.367 Hour3[1076:903] substring: loves the color

All applications use strings. They need them to display information to users and to gather information from users. As a Mac developer, you will find that you spend a great deal of time creating, manipulating, and comparing strings.

This leads to the next section on pointers and equality, which is very important to understand, especially when comparing things like strings.

  • + Share This
  • 🔖 Save To Your Account