Chapter 12. Using Strings

In This Chapter

  • Using the NSString and NSMutableString Classes

  • Understanding Format Strings

  • Using the Special Objective-C String Declaration Syntax

Any good standard library needs a great string class, and Objective-C with Foundation is no exception. In fact, the foundation framework comes with an excellent string class, NSString. Like many of the low-level core classes in Foundation, an immutable version of NSString, as well as a mutable version called NSMutableString exists. These two classes give you a tremendous amount of functionality when working with string values.

Understanding the String Declaration Syntax

Although NSString and NSMutableString have many types of initializers and factory methods available, strings are such a commonly used class in Objective-C, a special language construct has been created explicitly for the purposes of declaring a string easily. This construct is shown in Listing 12.1.

Example 12.1. The Objective-C NSString shortcut syntax

NSString *someString = @"this is a string";

Essentially, the compiler knows that any time it encounters @ and then a string contained within double quotes, it should create a static const NSString object to contain the string provided.

Any two declarations of the exact same string value, even if stored in different variable names, point to the same object. Therefore, you can use these strings for keys, for example, where the equality of the string as compared to another instance of that string will be considered ...

Get Objective-C 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.