Last active
April 11, 2017 15:02
-
-
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.
This file contains hidden or 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 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