ios - Swift - Typealias dictionary with value that implements a generic protocol -
i want typealias dictionary of string keys , values of objects/structs implements equatable protocol. wrote line of code gave me error didn't know how go on fix.
typealias storage = [string: equatable]
i want use type [string: equatable] variable in protocol, e.g:
protocol storagemodel { var storage: storage { set } init(storage: storage) }
error:
protocol 'equatable' can used generic constraint because has self or associated type requirements
can suggest solution?
generally speaking, protocol tag isn't required, protocol names first-class type names , can used directly:
typealias storage = [string:equatable]
in case, error telling because equatable includes func == (lhs:self, rhs:self) -> bool
, lhs:self
, equatable can't used except constraint on generic:
class generic<t:equatable> { ... }
without more details you're trying achieve , how you're trying use storagemodel, best can come is:
protocol matches { typealias t func matches(t:t) -> bool } protocol storagemodel { typealias t var storage: [string:t] { set } init(storage:[string:t]) } extension int : matches { func matches(target:int) -> bool { return self == target } } class myclass <t:matches> { var model = [string:t]() }
another possibility use generic instead of protocol:
class storagemodel <t:equatable> { var storage: [string:t] init(storage:[string:t]) { self.storage = storage } }
from there you'll need research, dig swift manual, googling , see solves problem.
Comments
Post a Comment