Created
March 10, 2016 19:42
-
-
Save waynebloss/c682c8949a89ddbbe443 to your computer and use it in GitHub Desktop.
How do I extend this dictionary?
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
import Foundation | |
protocol ComponentObject { | |
var name: String { get } | |
} | |
private var _idPrev = 0; | |
private func defaultName(baseName: String) -> String { | |
return baseName + String(++_idPrev) | |
} | |
class Thing : ComponentObject { | |
let name: String | |
init(name: String? = nil) { | |
self.name = name ?? defaultName("thing") | |
} | |
} | |
class OtherThing : ComponentObject { | |
let name: String | |
init(name: String? = nil) { | |
self.name = name ?? defaultName("other") | |
} | |
} | |
let w = Thing() | |
w.name | |
let m = OtherThing() | |
m.name | |
var d = [String: ComponentObject]() | |
d[w.name] = w | |
d[m.name] = m | |
let c = d["other2"]! | |
c.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I write the extension like this:
I get an error when I call getByPath: