:
let smaller : (inout _: [String : [String : [String :[String : String]]]], key: String, val: String) -> () = { dict, key, val in
dict["with"]?["complex"]?["sub"]?[key] = val
return ()
}
var a = [String : String]()
var b = [String :[String : String]]()
var c = [String : [String : [String : String]]]()
var myDictionary = [String : [String : [String :[String : String]]]]()
a["dictionary"] = "OldValue"
b["sub"] = a
b["anothersub"] = a
c["complex"] = b
myDictionary["with"] = c
print(myDictionary)
smaller(&myDictionary, key: "dictionary", val: "NewValue")
print(myDictionary)
, : , , (.. ).
let smaller2 : (String, String) -> () = { myDictionary["with"]?["complex"]?["sub"]?[$0] = $1 }
smaller2("dictionary", "NewerValue")
print(myDictionary)
/* ["with": ["complex": ["anothersub": ["dictionary": "OldValue"],
"sub": ["dictionary": "NewerValue"]]]] */
myDictionary , , , , " ", . "with.complex.sub", :
/* say 'myDictionary' is some class property (initialized as in example above)
In same class, introduce the following method */
func dictClosure(dictKeyPath: String) -> ((String, String) -> ()) {
let arr = dictKeyPath.componentsSeparatedByString(".")
if arr.count == 3 {
return {
myDictionary[arr[0]]?[arr[1]]?[arr[2]]?[$0] = $1 }
}
else {
return {
_, _ in
print("This closure is invalid")
}
}
}
/* example usage */
var smaller3 = dictClosure("with.complex.sub")
smaller3("dictionary", "NewestValue")
smaller3 = dictClosure("with.complex.anothersub")
smaller3("dictionary", "AlsoNewValue")
print(myDictionary)
/* ["with": ["complex": ["anothersub": ["dictionary": "AlsoNewValue"],
"sub": ["dictionary": "NewestValue"]]]] */
("one.two.three") .
, , smaller - , . . smaller3("dcitionary", "NewValue") - "dcitionary": "NewValue" . , ? smaller :
/* smaller ... */
dict["with"]?["complex"]?["sub"]?[key]? = val
/* smaller2 ... */
myDictionary["with"]?["complex"]?["sub"]?[$0]? = $1
/* smaller3 ... */
myDictionary[arr[0]]?[arr[1]]?[arr[2]]?[$0]? = $1