But not all such value objects necessarily benefit from having mutable versions. Objects that are good candidates for immutability are ones that encapsulate collections of discrete values or contain values that are stored in buffers (which are themselves kinds of collections, either of characters or bytes). Immutability is a guarantee that an object won’t unexpectedly change in value while you’re using it. The user selects a row corresponding to an object in the array that has been removed by some code elsewhere in the program, and problems ensue. Things can become even more dire if, for instance, you get a reference to an array that you use to populate a table view. Suddenly your label has changed out from under you. Now another subsystem in your application gets its own reference to that same string and decides to mutate it. You use this string in your user interface to identify a particular piece of data. In your application you invoke a method and are handed back a reference to an object representing a string. Why this complexity? What purpose does having an immutable variant of a mutable object serve?Ĭonsider a scenario where all objects are capable of being mutated. However, any Cocoa framework can potentially have its own mutable and immutable class variants.Īlthough these classes have atypical names, they are closer to the mutable norm than their immutable counterparts. Note: Except for NSMutableParagraphStyle in the AppKit framework, the Foundation framework currently defines all explicitly named mutable classes. The mutable subclasses are typically subclasses of their immutable superclass and have “Mutable” embedded in the class name. The Foundation framework adds some nuance to this picture by introducing classes that have mutable and immutable variants. A well-designed model object-say, an object representing a customer record- requires setter methods to change its instance data. For example, you can change the size, positioning, title, buffering behavior, and other characteristics of an NSWindow object. Most objects allow you to change their encapsulated data through setter accessor methods. Why Mutable and Immutable Object Variants? The following sections explain the reasons for having mutable and immutable variants of an object type, describe the characteristics and side-effects of object mutability, and recommend how best to handle objects when their mutability is an issue. But you can change the encapsulated value of a mutable object at any time. You cannot change the encapsulated values of immutable objects once such an object is created, the value it represents remains the same throughout the object’s life. In this example an array of key strings is created as well as an array of integers that will ultimately be used to create a new dictionary of even key / value pairs.Cocoa objects are either mutable or immutable. The fourth example is new to Swift 4 and came out of proposal 165. Notice that in the output that the "one" key contains the number one instead of two. This new Dictionary initializer now enhances dictionary uniqueness by using a closure to filter the first duplicate keys in a dictionary. If you notice, the first two tuples contain the "one" key which typically is not supported when creating a dictionary. In this example an array of tuples is created to simulate key/value pairs in a dictionary that contain conflicting keys. The third example is new to Swift 4 and came out of proposal 165. These examples often work well when manipulating small bits of data in your program. The second example creates a dictionary literal where the integer keys and values are explicitly assigned during the dictionary creation. After the dictionary is created on line 6 the subscript syntax is used to explicitly assign a value to a key. The first creates a new mutable dictionary type with a string definition as the key and value. The first two examples of dictionary creation may look familiar as these are Swift 3, Xcode 8 supported methods for dictionary creation.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |