I have this protocol
protocol JsonConvertable {
init?(_ underlyingValue: UnderlyingValue)
}
UnderlyingValue is an enum:
enum UnderlyingValue {
case string(String)
case int(Int)
case double(Double)
case bool(Bool)
case array(Array<Dictionary<String, AnyObject>>)
case dictionary(Dictionary<String, AnyObject>)
init?(value: JsonConvertable) {
switch rawValue {
case let value as [String: AnyObject]: self = .dictionary(value)
case let value as Array<[String: AnyObject]>: self = .array(value)
case let value as Double: self = .double(value)
case let value as Int: self = .int(value)
case let value as Bool: self = .bool(value)
case let value as String: self = .string(value)
default: return nil
}
}
}
I can extend most types as follows
extension String: JsonConvertable {
init?(_ underlyingValue: UnderlyingValue) {
switch underlyingValue {
case .bool(let value): self = value
default: return nil
}
}
However the Array extension is giving me the error
Cannot assign value of type 'Array<Dictionary<String, AnyObject>>' to type 'Array<_>'
extension Array: JsonConvertable {
init?(_ underlyingValue: UnderlyingValue) {
switch underlyingValue {
case .array(let value): self = value
default: return nil
}
}
}
I have tried everything I can think of but nothing is working. I tried to have JsonConvertable conform to ArrayLiteralConvertable, I have tried using generics in the init but I am just starting to understand generics and don't really know how it would be beneficial in this case, the same with AssociatedType. I have tried to constrict Element. I have been trying to get this to work for 2 whole days and everything I do seems to not make any headway. What am I missing?