So I'm trying to make a small dependency injection container using generics and protocol conformance. However i'm having some trouble compiling it.
I wonder if there is a swift expert that can tell me what the problem is.
import Foundation
protocol Service: NSObjectProtocol {
init()
}
protocol ServicesType {
func add<S: Service>(service: S.Type)
func get<S: Service>() throws -> S
}
class Services: ServicesType {
var services: [Service.Type] = []
func add<S: Service>(service: S.Type) {
if services.contains(where: { service -> Bool in return service == service }) { return }
services.append(service)
}
func get<S: Service>() throws -> S {
guard let first = services.first(where: { service -> Bool in return service == service }) else { throw ServicesTypeError.notFound }
return first.init() as! S
}
}
enum ServicesTypeError: Error {
case notFound
}
Generic parameter 'S' could not be inferred is the error message when i try to compile the line below
let creditCardService: CreditCardServiceType = try self.services.get()
protocol CreditCardServiceType : Service {
var cards: [CreditCard] { get }
}
class CreditCardService: CreditCardServiceType {
internal required init() {}
var cards: [CreditCard] = []
}
It took a little modification to get this working in a Playground, but this seems to work.
If I'm right (in bold),
let creditCardService: **CreditCardServiceType** = try self.services.get()
Needs to be:
let creditCardService: **CreditCardService** = try self.services.get()
Full code:
import Foundation
protocol Service: NSObjectProtocol {
init()
}
protocol ServicesType {
func add<S: Service>(service: S.Type)
func get<S: Service>() throws -> S
}
class Services: ServicesType {
var services: [Service.Type] = []
func add<S: Service>(service: S.Type) {
if services.contains(where: { service -> Bool in return service == service }) { return }
services.append(service)
}
func get<S: Service>() throws -> S {
guard let first = services.first(where: { service -> Bool in return service == service }) else { throw ServicesTypeError.notFound }
return first.init() as! S
}
}
enum ServicesTypeError: Error {
case notFound
}
class CreditCard {
}
protocol CreditCardServiceType : Service {
var cards: [CreditCard] { get }
}
class CreditCardService: NSObject, CreditCardServiceType {
required override init() {
super.init()
}
var cards: [CreditCard] = []
}
let services = Services()
services.add(service: CreditCardService.self)
let creditCardService: CreditCardService = try services.get()
Ok what i ended up with is the following. Not as type safe as i wanted but good enough.
import Foundation
protocol Service: NSObjectProtocol {
init()
}
protocol ServicesType {
func add<S: Service>(service: S.Type) throws
func get<S>() throws -> S
}
class Services: ServicesType {
var services: [Service.Type] = []
func add<S: Service>(service: S.Type) throws {
if services.contains(where: { existing -> Bool in return existing == service }) { throw ServicesTypeError.allReadyAdded}
services.append(service)
}
func get<S>() throws -> S {
guard let first = services.first(where: { existing -> Bool in return existing is S }) else { throw ServicesTypeError.notFound }
return first.init() as! S
}
}
enum ServicesTypeError: Error {
case allReadyAdded
case notFound
}
usage:
try self.services.add(service: ProfileService.self)
let profileService: ProfileServiceType = try self.services.get()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.