Skip to content

Instantly share code, notes, and snippets.

@ranmyfriend
Last active April 11, 2017 15:02
Show Gist options
  • Save ranmyfriend/265ead29c4e0af8cf2c951d2812cab16 to your computer and use it in GitHub Desktop.
Save ranmyfriend/265ead29c4e0af8cf2c951d2812cab16 to your computer and use it in GitHub Desktop.
Here you can create a Dictionary key can hold any kind of object or value or enum. Here, DataSourceType.dictionary key is Generic.
import UIKit
import Foundation
enum Gender:Int {
case male,female,other
}
class Department:Hashable,Equatable{
var uid: Int
var hashValue: Int {
return self.uid
}
init(uid: Int) {
self.uid = uid
}
public static func ==(lhs: Department, rhs: Department) -> Bool {
return lhs.uid == rhs.uid
}
}
class People {}
class DataSourceType<T:Hashable> {
var dictionary:[T:[People]]?
init() {
dictionary = [:]
}
}
var datasource:Any? = nil
datasource = DataSourceType<Department>()
var ds = datasource as! DataSourceType<Department>
let d = Department.init(uid: 121)
ds.dictionary?[d] = [People()]
datasource = DataSourceType<Gender>()
var genderSource = datasource as! DataSourceType<Gender>
genderSource.dictionary?[.male] = [People()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment