Можно ли как-то оптимизировать код, убрав определение ключей dictionary[“key”]?
struct Cat {
var name: String
var country: String
}
let first = Cat(name: "First", country: "Russia")
let second = Cat(name: "Second", country: "USA")
let third = Cat(name: "Third", country: "England")
let cats = [first, second, third]
var dictionary: [String: [Cat]] = [:]
dictionary["England"] = []
dictionary["USA"] = []
dictionary["Russia"] = []
for element in cats {
dictionary[element.country]?.append(element)
}