Mutable Versus Immutable Objects

When you create a string object by writing an expression such as

@"Programming is fun"

you create an object whose contents cannot be changed. This is referred to as an immutable object. The NSString class deals with immutable strings. You will often want to deal with strings whose characters you can change. For example, you might want to delete some characters from a string or perform a search-and-replace operation on a string. You handle these types of strings through the NSMutableString class.

Program 15.4 shows basic ways to work with immutable character strings in your programs.

Program 15.4

// Basic String Operations#import <Foundation/Foundation.h>int main (int argc, char * ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.