48 Using value types can result in significant performance improvements in user

48 using value types can result in significant

This preview shows page 8 - 10 out of 20 pages.

[48] Using value types can result in significant performance improvements in user applications as well. [49] To ensure that even the largest structs do not cause a performance penalty when they are handed off, Swift uses copy on write so that the objects are copied only if and when the program attempts to change a value in them. This means that the various accessors have what is in effect a pointer to the same data storage, but this takes place far below the level of the language, in the computer's memory management unit (MMU). So while the data is physically stored as one instance in memory, at the level of the application, these values are separate, and physical separation is enforced by copy on write only if needed. [50] Protocol-oriented programming [ edit ] A key feature of Objective-C is its support for categories , methods that can be added to extend classes at runtime. Categories allow extending classes in-place to add new functions with no need to subclass or even have access to the original source code . An example might be to add spell checker support to the base NSString class, which means all instances of NSString in the application gain spell checking. The system is also widely used as an organizational technique, allowing related code to be gathered into library-like extensions. Swift continues to support this concept, although they are now termed extensions , and declared with the keyword extension . Unlike Objective-C, Swift can also add new properties accessors, types and enums to extant instances. Another key feature of Objective-C is its use of protocols , known in most modern languages as interfaces . Protocols promise that a particular class implements a set of methods, meaning that other objects in the system can call those methods on any object supporting that protocol. This is often used in modern OO languages as a substitute for multiple inheritance , although the feature sets are not entirely similar. A common example of a protocol in Cocoa is the NSCopying protocol, which defines one method, copyWithZone , that implements deep copying on objects. [51] In Objective-C, and most other languages implementing the protocol concept, it is up to the programmer to ensure that the required methods are implemented in each class. [52] Swift adds the ability to add these methods using extensions, and to use generic programming (generics) to implement them. Combined, these allow protocols to be written once and support a wide variety of instances. Also, the extension mechanism can be used to add protocol conformance to an object that does not list that protocol in its definition. [51] For example, a protocol might be declared called StringConvertible , which ensures that instances that conform to the protocol implement a toString method that returns a String . In Swift, this can be declared with code like this: protocol StringConvertible { func toString () -> String } This protocol can now be added to String, with no access to the base class's source: extension String : StringConvertible { func toString () -> String { return self
Image of page 8
} }
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 20 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes