swift - How do I implement this protocol in struct -
i'm new swift , want create abstract factory db access. here protocol
protocol idaofactory { associatedtype dao: idao func createaccountdao<dao: iaccountdao>() -> dao } struct realmfactory: idaofactory { } protocol idao { associatedtype t func save(object: t) } protocol iaccountdao : idao { } struct accountdaorealm: iaccountdao { }
how implement idaofactory in struct realmfactory , iaccountdao in struct accountdaorealm? can help?
generics in swift have many restrictions when used in protocols , implemented in struct. let's wait until swift 3 :)
i use protocols , derived classes or generics classes mixing protocols generics , structs makes headache in swift 2 (c# generics in more convenient)
i played code in playground, here is
protocol idaofactory { associatedtype dao: idao func createaccountdao<dao: iaccountdao>() -> dao } protocol idao { init() associatedtype t func save(object: t) } protocol iaccountdao : idao { init() } public class accountdaorealm: iaccountdao { var data: string = "" required public init() { data = "data" } func save(object: accountdaorealm) { //do save } } let accountdaorealm = accountdaorealm() //as see accountdaorealm constructed without error struct realmfactory: idaofactory { func createaccountdao<accountdaorealm>() -> accountdaorealm { return accountdaorealm() //but here constructor gives error } }
Comments
Post a Comment