Last active
August 15, 2016 20:03
-
-
Save marianolatorre/221fe5185c51671fa3ba363620edeeda to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// using basic protocols, compliaing to many and static | |
// | |
protocol TestProtocol { | |
var forcedSet: Int { get set } | |
var noNeedToSet: Int { get } | |
} | |
protocol TestTypeProtocol { | |
static var staticTypeProperty: String { get set } | |
static var staticOptionalProperty: String? { get set } | |
} | |
protocol FullyNamed { | |
var fullName: String { get } | |
} | |
struct Person : FullyNamed, TestTypeProtocol { | |
var fullName: String | |
static var staticTypeProperty: String = "" // this ones needs an initial value | |
static var staticOptionalProperty: String? // this one is optional, no need for | |
} | |
// | |
// overriding and complaining protocol with the same method: | |
// | |
protocol SomeProtocol { | |
init() | |
} | |
class SomeSuperClass { | |
init() { | |
// initializer implementation goes here | |
} | |
} | |
class SomeSubClass: SomeSuperClass, SomeProtocol { | |
// "required" from SomeProtocol conformance; "override" from SomeSuperClass | |
override required init() { | |
// initializer implementation goes here | |
} | |
} | |
// | |
// protocols as a type | |
// | |
protocol RandomNumberGenerator { | |
func random() -> Double | |
} | |
class Dice { | |
let sides: Int | |
let generator: RandomNumberGenerator | |
init(sides: Int, generator: RandomNumberGenerator) { | |
self.sides = sides | |
self.generator = generator | |
} | |
func roll() -> Int { | |
return Int(generator.random() * Double(sides)) + 1 | |
} | |
} | |
class SpecialTenGenerator: RandomNumberGenerator { | |
func random() -> Double { | |
return 10 | |
} | |
} | |
var d6 = Dice(sides: 6, generator: SpecialTenGenerator()) | |
for _ in 1...5 { | |
print("Random dice roll is \(d6.roll())") | |
} | |
// | |
// using protocols for delegation | |
// | |
protocol DiceGame { | |
var dice: Dice { get } | |
func play() | |
} | |
protocol DiceGameDelegate { | |
func gameDidStart(game: DiceGame) | |
func gameDidEnd(game: DiceGame) | |
func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int) | |
} | |
class BestDiceGame : DiceGame { | |
let dice = Dice(sides: 6, generator: SpecialTenGenerator()) | |
init () {} | |
var delegate : DiceGameDelegate? | |
func play() { | |
let diceRoll = dice.roll() | |
delegate?.gameDidStart(self) | |
// do stuff | |
print(diceRoll) | |
delegate?.gameDidEnd(self) | |
} | |
} | |
class DiceGameTracker: DiceGameDelegate { | |
var variable = 0 | |
func gameDidEnd(game: DiceGame) { | |
// if games end | |
} | |
func gameDidStart(game: DiceGame) { | |
// when game starts | |
} | |
func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int) { | |
// new turn | |
} | |
} | |
let tracker = DiceGameTracker() | |
let game = BestDiceGame() | |
game.delegate = tracker | |
game.play() | |
// | |
// protocol conformance with extension | |
// | |
protocol TextRepresentable { | |
var textualDescription: String { get } | |
} | |
extension Dice: TextRepresentable { | |
var textualDescription: String { | |
return "A \(sides)-sided dice" | |
} | |
} | |
extension BestDiceGame: TextRepresentable { | |
var textualDescription: String { | |
return "A game with a dices" | |
} | |
} | |
// | |
// Protocol adoption with extension | |
// | |
struct Cat { | |
var name : String | |
var lifes : Int | |
var textualDescription: String { | |
return "A very nice dog!" | |
} | |
} | |
struct Dog { | |
var name : String | |
var textualDescription: String { | |
return "A very nice dog!" | |
} | |
} | |
extension Dog : TextRepresentable {} | |
extension Cat : TextRepresentable {} | |
let myDog = Dog(name: "my dog") | |
let cat = Cat(name: "kitty", lifes: 7) | |
// | |
// Array of protocol types | |
// | |
let printableThings : [TextRepresentable] = [myDog, cat] | |
// | |
// Protocol composition | |
// | |
protocol Named { | |
var name: String { get } | |
} | |
protocol Aged { | |
var age: Int { get } | |
} | |
struct NamedAgedPerson: Named, Aged { | |
var name: String | |
var age: Int | |
} | |
func wishHappyBirthday(celebrator: protocol<Named, Aged>) { | |
print("Happy birthday \(celebrator.name) - you're \(celebrator.age)") | |
} | |
var birthdayPerson = NamedAgedPerson(name: "Mariano", age: 18) // hehe! | |
wishHappyBirthday(birthdayPerson) | |
print(birthdayPerson is Aged) | |
print(birthdayPerson is TextRepresentable) | |
if let text = birthdayPerson as? TextRepresentable { | |
print(text) | |
} | |
if let text = birthdayPerson as? Aged { | |
print(text.age) | |
} | |
class Animal {} | |
class Shark: Animal {} | |
let a: Animal = Shark() | |
//a as Shark // now raises the error: "'Animal is not convertible to 'Dog'; | |
// ... did you mean to use 'as!' to force downcast?" | |
print (a) | |
var c = a as! Shark // forced downcast is allowed | |
print(c) | |
let d = Shark() | |
d as Animal // upcast succeeds | |
protocol HasArea { | |
var area: Double { get } | |
} | |
class Circle: HasArea { | |
let pi = 3.1415927 | |
var radius: Double | |
var area: Double { return pi * radius * radius } | |
init(radius: Double) { self.radius = radius } | |
} | |
class Country: HasArea { | |
var area: Double | |
init(area: Double) { self.area = area } | |
} | |
class Animal2 { | |
var legs: Int | |
init(legs: Int) { self.legs = legs } | |
} | |
let objects: [AnyObject] = [ | |
Circle(radius: 2.0), | |
Country(area: 243_610), | |
Animal2(legs: 4) | |
] | |
for object in objects { | |
if let objectWithArea = object as? HasArea { | |
print(objectWithArea.area) | |
} | |
else { | |
print("this doesnt have area") | |
} | |
} | |
// | |
// Protocol extensions | |
// | |
extension RandomNumberGenerator { | |
func randomBool() -> Bool { | |
return random() > 0.5 | |
} | |
func random() -> Double { | |
return random() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment