Created
April 22, 2016 20:47
-
-
Save SBarkovskiy/c3a51851013a875411f313e216979eff to your computer and use it in GitHub Desktop.
Barkovsky
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Protocols and Extensions | |
Use protocol to declare a protocol. | |
protocol ExampleProtocol { | |
var simpleDescription: String { get } | |
mutating func adjust() | |
} | |
Classes, enumerations, and structs can all adopt protocols. | |
class SimpleClass: ExampleProtocol { | |
var simpleDescription: String = "A very simple class." | |
var anotherProperty: Int = 69105 | |
func adjust() { | |
simpleDescription += " Now 100% adjusted." | |
} | |
} | |
var a = SimpleClass() | |
a.adjust() | |
let aDescription = a.simpleDescription | |
struct SimpleStructure: ExampleProtocol { | |
var simpleDescription: String = "A simple structure" | |
mutating func adjust() { | |
simpleDescription += " (adjusted)" | |
} | |
} | |
var b = SimpleStructure() | |
b.adjust() | |
let bDescription = b.simpleDescription | |
enum Family: ExampleProtocol { | |
case Dother, Son | |
var simpleDescription: String { | |
get { | |
switch self { | |
case .Dother: | |
return "A family enum: Dother" | |
case .Son: | |
return "A family enum: Son" | |
} | |
} | |
} | |
mutating func adjust() { | |
switch self { | |
case .Dother: | |
self = Dother | |
case .Son: | |
self = Son | |
} | |
} | |
} | |
var love = Family.Dother | |
love.simpleDescription | |
love.adjust() | |
love.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. | |
extension Int: ExampleProtocol { | |
var simpleDescription: String { | |
return "The number \(self)" | |
} | |
mutating func adjust() { | |
self += 42 | |
} | |
} | |
print(7.simpleDescription) | |
extension Double { | |
var apple: Double { | |
get { | |
return (self) | |
} | |
} | |
} | |
let AppleDouble = 3 | |
print (3.apple) | |
Experiment: Write an extension for the Double type that adds an absoluteValue property. | |
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. | |
let protocolValue: ExampleProtocol = a | |
print(protocolValue.simpleDescription) | |
// print(protocolValue.anotherProperty) // Uncomment to see the error | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment