Swift supports multiple subscribers, so you can use this to define access-insensitve accessor:
extension Dictionary where Key : StringLiteralConvertible {
subscript(ci key : Key) -> Value? {
get {
let searchKey = String(key).lowercaseString
for k in self.keys {
let lowerK = String(k).lowercaseString
if searchKey == lowerK {
return self[k]
}
}
return nil
}
}
}
let dict = [
"name": "John",
"location": "Chicago",
]
print(dict[ci: "NAME"])
print(dict[ci: "lOcAtIoN"])
Dictionary
, Key
String
( ). Swift struct
. String
- StringLiteralConvertible
.
, 2 , , , :
let dict = [
"name": "John",
"NAME": "David",
]
print(dict[ci: "name"])