Last active
August 8, 2016 21:55
-
-
Save plivesey/ada094dd80ba737d2d50140530bbd74d to your computer and use it in GitHub Desktop.
See how many you can get right before running it... :D
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
//: Playground - noun: a place where people can play | |
import UIKit | |
// PROTOCOL | |
protocol Foo { | |
func foo() -> String | |
func baz() -> String | |
} | |
extension Foo { | |
func foo() -> String { | |
return "foo" | |
} | |
func bar() -> String { | |
return "bar" | |
} | |
func baz() -> String { | |
return "baz" | |
} | |
} | |
// STRUCT | |
class MyFoo: Foo { | |
func foo() -> String { | |
return "myfoo" | |
} | |
func bar() -> String { | |
return "mybar" | |
} | |
} | |
class MySubFoo: MyFoo { | |
func foo() -> String { | |
return "mysubfoo" | |
} | |
func baz() -> String { | |
return "mybaz" | |
} | |
} | |
// CODE | |
// An instance of MyFoo | |
let superA = MyFoo() | |
// Same thing, but cast to Foo (the protocol) | |
let superB: Foo = superA | |
// An instance of MySubFoo | |
let subA: MySubFoo = MySubFoo() | |
// Same thing, but cast to MyFoo (the superclass) | |
let subB: MyFoo = subA | |
// Same thing, but cast to Foo (the protocol) | |
let subC: Foo = subA | |
superA.foo() // = ? | |
superB.foo() // = ? | |
subA.foo() // = ? | |
subB.foo() // = ? | |
subC.foo() // = ? | |
superA.bar() // = ? | |
superB.bar() // = ? | |
subA.bar() // = ? | |
subB.bar() // = ? | |
subC.bar() // = ? | |
superA.baz() // = ? | |
superB.baz() // = ? | |
subA.baz() // = ? | |
subB.baz() // = ? | |
subC.baz() // = ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment