Created
October 22, 2015 08:29
-
-
Save angerman/76b72fcbc027d3420fb5 to your computer and use it in GitHub Desktop.
EXC_BAD_ACCESS
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
// Let’s alias a function that takes an int and does nothing with it. | |
typealias Action = Int -> () | |
// If something has an action, it's Actionable, no? I think so. But we’ll leave it optional if there actually *is* an action. | |
protocol Actionable { | |
var action: Action? { get } | |
} | |
// And obviously we need Things. We always need Things. | |
protocol Thing {} | |
// Now Items are Things that are Actionable. | |
struct Item: Thing, Actionable { | |
let action: Actionable? | |
} | |
// To get things, we’ll define some protocol to give us Things. | |
protocol GimmeThing { | |
func thing(index:Int) -> Thing | |
} | |
// A List is something that has Things. | |
struct List { | |
var items:[Thing] | |
} | |
// Clearly we can make List give us Things. So it can adhere to GimmeThing. | |
extension List: GimmeThing { | |
func thing(index: Int) -> Thing { | |
return self.items[index] | |
} | |
} | |
func bad_access() { | |
// lets have two Items, one with and one without an action. | |
let C = Item(action: nil) | |
let C2 = Item(action: { print($0) }) | |
// And stuff them into a List | |
let lst = List(items: [C,C2]) | |
// But hey, we forgot, let’s see which of those things is actually Actionable. | |
for i in 0..<2 { | |
if let c = lst.thing(i) as? protocol<Actionable, Thing> { | |
c.action?(1) | |
} | |
} | |
} | |
bad_access() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment