let dic: Dictionary<String, Int> = ["key1": 0, "key2": 1]
dic["key1"] // 0
let arr: Array<String> = ["value1", "value2"]
arr[0] // "value1"
struct Member {
var id: Int
var name: String
subscript(script: String) -> Result<String, Error> {
return script == "id" ? .success(String(id)) : .failure(NSError(domain: "no exact data", code: 1))
}
}
let member1 = Member(id: 129012, name: "member1")
member1["id"] // .success(129012)
member1["name"] // .failure(NSError)
@dynamicMemberLookup
struct Member {
var id: Int
var name: String
subscript(dynamicMember script: String) -> Result<String, Error> {
return script == "ids" ? .success(String(id)) : .failure(NSError(domain: "no exact data", code: 1))
}
}
let member1 = Member(id: 129012, name: "member1")
member1.ids // .success(129012)
member1.names // .failure(NSError)