I've got a method which calls method of some manager to save int value with some key. My method receives int and some EnumKey enum value as a key, extrudes EnumKey's rawValue and passes it to a manager as a string:
set(value: Int, forKey key: EnumKey) {
SomeManager.saveIntValueWithStringKey(valueToSave: value, keyToSave: key.rawValue)
}
enum EnumKey: String {
case One="first key"
case Two="second key"
}
I want to make this more generic by allow my method to receive every enum with string raw value instead of EnumKey. In implementation of method I've replaced type of key parameter from EnumKey to GenericKey protocol, and made EnumKey conform this protocol:
set(value: Int, forKey key: GenericKey) {
SomeManager.saveIntValueWithStringKey(valueToSave: value, keyToSave: key.rawValue)
}
protocol GenericKey {
var rawValue: String { get }
}
enum EnumKey: String, GenericKey {
case One="first key"
case Two="second key"
}
But this String, GenericKey
looks kinda ugly. I want every string-representable enum to suit automatically without mentioning it conforms to GenericKey protocol in addition to RawRepresentable and String raw type. Something like:
protocol GenericKey: RawRepresentable {
associatedtype RawValue = String
}
but compiler says "Protocol can be used only as a generic constraint because it has Self or associated type requirements".
What could be an easy way to explain compiler that protocol describes only RawRepresentable things with RawValue of String type?
You can define the function as generic, and define the generic type as
RawRepresentable
withRawValue
of typeString
, like this: