The Swift Programming Language (Swift 2) -5 Objects and Classes
21 September 2015
//: ## Protocols and Extensions//://: Use `protocol` to declare a protocol.//:protocolExampleProtocol{varsimpleDescription:String{get}mutatingfuncadjust()}//: Classes, enumerations, and structs can all adopt protocols.//:classSimpleClass:ExampleProtocol{varsimpleDescription:String="A very simple class."varanotherProperty:Int=69105funcadjust(){simpleDescription+=" Now 100% adjusted."}}vara=SimpleClass()a.adjust()letaDescription=a.simpleDescriptionstructSimpleStructure:ExampleProtocol{varsimpleDescription:String="A simple structure"mutatingfuncadjust(){simpleDescription+=" (adjusted)"}}varb=SimpleStructure()b.adjust()letbDescription=b.simpleDescription//: > **Experiment**://: > Write an enumeration that conforms to this protocol.//://: Notice the use of the `mutating` keyword in the declaration of `SimpleStructure` to mark a method that modifies the structure. The declaration of `SimpleClass` doesn’t need any of its methods marked as mutating because methods on a class can always modify the class.//://: Use `extension` to add functionality to an existing type, such as new methods and computed properties. You can use an extension to add protocol conformance to a type that is declared elsewhere, or even to a type that you imported from a library or framework.//:extensionInt:ExampleProtocol{varsimpleDescription:String{return"The number \(self)"}mutatingfuncadjust(){self+=42}}print(7.simpleDescription)//: > **Experiment**://: > Write an extension for the `Double` type that adds an `absoluteValue` property.//://: You can use a protocol name just like any other named type—for example, to create a collection of objects that have different types but that all conform to a single protocol. When you work with values whose type is a protocol type, methods outside the protocol definition are not available.//:letprotocolValue:ExampleProtocol=aprint(protocolValue.simpleDescription)// print(protocolValue.anotherProperty) // Uncomment to see the error//: Even though the variable `protocolValue` has a runtime type of `SimpleClass`, the compiler treats it as the given type of `ExampleProtocol`. This means that you can’t accidentally access methods or properties that the class implements in addition to its protocol conformance.//://: [Previous](@previous) | [Next](@next)