The Foundation framework contain many classes and methods for instantiating (creating) and manipulate object variables. When you create an iOS project, Xcode adds a folder called Frameworks in the Navigator panel as shown in the image on the left. Under that folder you will find three sub folders listed there. The Headers folder shown in the image, contain an alphabetical listing of classes you can use in an iOS project. One of them is the NSString class.

The NSString class enables you to instantiate (create) string objects (or variables). Instances of the NSString class are immutable. This mean, once you’ve created and initialize a string object, you can’t add or remove strings from it. An Objective-C string object can contain letters, numbers or characters. The NSString class is a subclass of the NSObject superclass and it inherits all methods of the NSObject class.


To practice source code presented in this lesson, you should download and unzip this file.
Launch the project (CodePractice.xcodeproj) and enter code in the viewController.m file’s buttonTapped method, then click the “Click Me” button to execute the method’s code.

Create and Display an NSString Object

Here are two ways to instantiate (create) and initialize a string object using the NSString class, then assign it to the UITextField control.

// first method
NSString *sentenceOne = @"When in doubt, don't.";

// second method
NSString *sentenceTwo = [[NSString alloc] init];
sentence = @"Your smile is very contagious";

self.inputBoxOne.text = sentenceOne;

The string @"When in doubt, don't." is a string constant and you can display it in the Debugger window and even a label control like this:

NSLog(@"When in doubt, don't.");
self.outputLabel.text = @"When in doubt, don't.";

Common NSString Methods

The NSString class contain several methods for manipulating instances (objects) of the NSString class. In this section I show you how to use a handful of them.

The stringWithFormat: method

The stringWithFormat: method is for creating a new object based on the arguments you provide. It is also used for converting a C style string to an Objective-C string object.

stringWithFormat: format, argument, ...

format This is where you place string constants and data type specifier for one or more arguments. Here is a list of format specifiers you can use.

Data Type Specifier Display
%i Integer
%c Character
%f Floating-point decimal
%s C style string
%@ Objective-C string
argument This is where you list string constants, variables, or both to be replaced by a data type specifier.
This is where you place additional comma-separated arguments.

They say a picture is worth a thousand words, well this one should shed some light on how the stringWithFormat: method works.

Example: C String → NSString Object

// Create and initialize a C string variable
const char *sentence = "I need a good book to read and $2.97 for Joey.";

// Convert the C string to an NSString object, then assign it to a UI control
self.outputBox.text = [NSString stringWithFormat:@"%s", sentence];

The UTF8String Method

This method converts an instance of the NSString class to a C string.

Example: NSString Object → C String

const char *childrenAdage = [@"finders keepers, losers weepers" UTF8String];

The floatValue Method

This method converts an NSString object, containing digits to a C floating-point variable. One reason you’d want to do that is because a UI control is an object and it’s data type is of NSString. This mean in order to perform calculation with numbers entered in a UI control, you must convert it from an object variable to a C style variable. Once you’ve perform calculation, you must convert calculation result to an NSString object before assigning it to a UI control.

Example: NSString Object → C Variable

Code Output
float prodPrice = [self.inputBoxOne.text floatValue];
float calcResult = (prodPrice * .25) + prodPrice;
self.outputBox.text = [NSString stringWithFormat:@"Calculation result %.2f", calcResult];

This table list other methods you can use to convert NSString objects, containing numbers, to C variables.

Method Purpose Example of Use
doubleValue This method converts a string object to a C double variable. double prodPrice = [@”3737.07″ doubleValue];
intValue This method converts a string object to a C integer variable. int prodPrice = [self.outputLabel.text int];
integerValue The NSInteger is not an Objective-C object. It is synonyms to C’s long int data type; so this method converts it to a C long integer variable. NSInteger longIntegerVariable = [@”4879788″ integerValue];

The stringByAppendingString: Method

This method joins two strings.

// Declare and initialize two string objects
NSString *stringOne = @"Bananas are a good source of vitamin C, ";
NSString *stringTwo = @"potassium and dietary fiber.";

// Join them, then assign them to the UITextView control
self.outputBox.text = [stringOne stringByAppendingString:stringTwo];

Bananas are a good source of vitamin C, potassium and dietary fiber.

The stringByAppendingFormat: Method

Use this method to kill two birds with one stone-join and format two strings.

NSString *stringOne = @"What time is it?";
NSString *output = [stringOne stringByAppendingFormat:@" It is %1d:%2d am.",7,27];
self.outputBox.text = output;

What time is it? It is 7:27 am.

The length Method

Use this method to get the total number of characters there are in an NSString object.

// Get string length
int totalCharacters = [@"Girl, your hair is on fire!" length];

// Display string length
self.inputBoxOne.text = [NSString stringWithFormat:@"Total characters: %i", totalCharacters];

Total characters: 27

You could use the length method in an expression like this:

Code Output
if ([self.inputBoxOne.text length] < 7) {
   self.outputBox.text = @"Your Password is to short. It must contain a minimum of 7 characters. Please try again.";
} else {
   self.outputBox.text = @"Login Successful.";

The isEqualToString Method

This method compares two NSString string objects to see if they are the same.

Code Output
NSString *stringOne = inputBoxOne.text;
NSString *stringTwo = inputBoxTwo.text;

if([stringOne isEqualToString:stringTwo])
   self.outputBox.text = @"Strings are the same.";
   self.outputBox.text = @"Strings are not the same.";