Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Last active August 29, 2015 14:24
Show Gist options
  • Save joanmolinas/160ae6c8192812bf06b2 to your computer and use it in GitHub Desktop.
Save joanmolinas/160ae6c8192812bf06b2 to your computer and use it in GitHub Desktop.
/* ClassCluster Example */
/* Documentation: https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html#//apple_ref/doc/uid/TP40010810-CH4 */
/* This example explicated in Objc: http://stackoverflow.com/questions/1844158/what-exactly-is-a-so-called-class-cluster-in-objective-c */
enum Types {
case Guffaw
case Giggle
}
protocol ClassClusterExample {
func ftw() -> String
}
class Laugh : ClassClusterExample {
func initWithType(type : Types) -> ClassClusterExample {
var c : ClassClusterExample!
switch type {
case .Guffaw:
c = Guffaw()
case .Giggle:
c = Giggle()
}
return c
}
func ftw() -> String {
return "Laugh"
}
}
class Guffaw : Laugh{
override func ftw() -> String {
return "Guffaw"
}
}
class Giggle : Laugh {
override func ftw() -> String {
return "Giggle"
}
}
var guffaw = Laugh().initWithType(.Guffaw)
guffaw.ftw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment