I'm trying to inject some values inside the [String: Any]
for later use.
Minimalistic example of my code:
var info = ["Person" : [
"Age" : 20,
"Name" : "Ratul Sharker"
]]
print(info)
if var person = info["Person"] as? [String:Any] {
person["height"] = 5.5
print("~~> Height injected")
//
// however if i reassign the
// person into the info, then
// the info appear into the print
// info["Person"] = person
}
print(info)
The code is also shared here
Output of this code gives me
["Person": ["Age": 20, "Name": "Ratul Sharker"]]
~~> Height injected
["Person": ["Age": 20, "Name": "Ratul Sharker"]]
Clearly the height
i am injecting has no effect, because when the optional unwrapping is happening it creates another copy of the person
dictionary. So the change remains the local. Assigning the local person into the dictionary do the trick.
Here what i want is not copying the value while optional unwrapping using if let
. Is there any way that i can tell if let
construction so that it take the reference of the value instead of value. I have searched a lot about this scenario, all i get the pass by reference to a function using &
and inout
.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire