Skip to content

Instantly share code, notes, and snippets.

@kmav
Created March 18, 2019 07:20
Show Gist options
  • Save kmav/629b6c005e378ef1bb1512770b9a6775 to your computer and use it in GitHub Desktop.
Save kmav/629b6c005e378ef1bb1512770b9a6775 to your computer and use it in GitHub Desktop.
Swift: Generics with custom type passing and definition
import UIKit
class BaseClass {
let name = "BaseClass"
var key = 0
required init() {
key = 100
}
}
protocol MappingModel {
}
protocol APIClient {
}
class Type1 : BaseClass {
required init() {
super.init()
key = 1
}
}
class Type2 : BaseClass {
required init() {
super.init()
key = 2
}
}
struct SetupTableClient<T : BaseClass>: APIClient {
func fetchSetupTable<T>(_ type: T.Type) where T:BaseClass, T:MappingModel {
self.saveSingleTable(type)
}
func saveSingleTable<T>(_ type: T.Type) where T:BaseClass, T:MappingModel {
let tent = T()
print(tent.name + " \(tent.key)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment