I have created a protocol as
protocol DataBaseManager{ associatedtype type init() func insert(object obj: [type]) func getObject(ofType type: type.Type)-> Array<type> //... }
Now i have defined a RealmManager as
class RealmManager: DataBaseManager{ var realm:Realm! required init() { do{ realm = try Realm() }catch { print(error) } } func insert(object obj: [Object]) { do { try realm.write { realm.add(obj) } } catch let error { print("the error in creation of object is \(error)") } } func getObject(ofType type: Object.Type)-> Array<Object> { let objects = realm.objects(type) return Array(objects) } //..... }
The whole point in doing so is to replace database later on To SqliteManager with ease if necessary. For that i am sending the dependency in my viewModel as:
struct LoginViewModel { var dbManager:DataBaseManager //error in this line init(dbManager: DataBaseManager) { //..... self.dbManager = dbManager } }
and calling it as
let loginViewModel = LoginViewModel(dbManager:RealmManager())
But i am getting error as
Protocol ‘DataBaseManager’ can only be used as a generic constraint because it has Self or associated type requirements
Since swift doesnot support to inject protocol with associated types. What could be the possible tweak to solve this?