I have the following protocol:
protocol MyProtocol {
var stringValue: String { get }
}
I also implemented it's methods for some classes and structures in extensions:
extension Int: MyProtocol {
var stringValue: String {
return "IntValue"
}
}
extension String: MyProtocol {
var stringValue: String {
return "StringValue"
}
}
extension Array: MyProtocol where Element == Dictionary<String, Any> {
var stringValue: String {
return "ArrayValue"
}
}
extension Dictionary: MyProtocol where Key == String, Value == Any {
var stringValue: String {
return "DictionaryValue"
}
}
When I tried to test it with the following code:
let dict = [["key":"value"]]
let stringValueResult = dict.stringValue
print(stringValueResult)
I received an error with text "[[String : String]]
is not convertible to Array<Dictionary<String, Any>>
". However my code works fine when I set type of the variable dict
like that:
let dict: Array<Dictionary<String, Any>> = [["key":"value"]]
Can someone explain me why the first version of my code is not compiling?