Created
March 18, 2019 07:20
-
-
Save kmav/629b6c005e378ef1bb1512770b9a6775 to your computer and use it in GitHub Desktop.
Swift: Generics with custom type passing and definition
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 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