- NSString is the Cocoa object version of a string.
- NSString and Swift String are bridged to one another, and you will often move between them without thinking, passing a Swift String to Cocoa where an NSString is expected, calling Cocoa NSString methods on a Swift String, and so forth.
let s = “hello”
let s2 = s.capitalized
- In that code, s is a Swift String and s2 is a Swift String, but the capitalized property actually belongs to Cocoa.
- In the course of that code, a Swift String has been bridged to NSString and passed to Cocoa, which has processed it to get the capitalized string; the capitalized string is an NSString, but it has been bridged back to a Swift String.
- In some cases, Swift may fail to cross the bridge implicitly for you, and you will need to cross the bridge yourself by casting explicitly.
- For example, if s is a Swift string, you can’t call appendingPathExtension on it directly:
- let s = “MyFile”
- let s2 = s.appendingPathExtension(“txt”) // compile error
- You have to cast explicitly to NSString.
- Another important difference between a Swift String and a Cocoa NSString is that an NSString is immutable.
- This means that, with NSString, you can do things such as obtain a new string based on the first — as capitalized and substring(to:) do — but you can’t change the string in place.
- To do that, you need another class, a subclass of NSString, NSMutableString.
- NSMutableString – Should be used when you are physically changing the value of an existing string, without completely discarding the old value (i.e. adding a character to the beginning or end, modifying a character in the middle etc).
- Also, NSMutableString is a subclass of NSString. So any method which can take an NSString can also take an NSMutableString.
- NSArray is Objective-C’s array object type.
- It is fundamentally similar to Swift Array, and they bridged to one another. But NSArray elements must be objects (classes and class instances), and they don’t have to of a single type.
- An NSArray’s length is its count, and an element can obtain by index number using the object(at:).
- The index of the first element, as with a Swift Array, is zero, so the index of the last element is count minus one.
- Instead of calling object(at:), you can use the subscription with an NSArray.
- You can seek an object within an array with index(of:) or indexOfObject- Identical(to:) If the object is not found in the array, the result is NSNotFound.
- Unlike a Swift Array, and like an Objective-C NSString, an NSArray immutable.
- This doesn’t mean you can’t mutate any of the objects it contains; it means that once the NSArray formed you can’t remove an object from it, insert an object into it, or replace an object at a given index.
- To do those things while staying in the Objective-C world. You can derive a new array consisting of the original array plus or minus some objects, or use NSArray’s subclass, NSMutableArray.
- The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects.
- This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray.