'Conditional cast always succeed in swift dict
Hello I have a little problem. I am triying to build a swift dictionnary that will later be translated to json for a network code.
A simpliflied version of the code is here :
let result: [String: AnyHashable] = [:]
var someInt = 0
result["key1"] = [
"A" : someInt,
"obj": [
"obj1": "a string",
"obj2": "otherString"
]
] as? [String: AnyHashable]
using the as? [String: AnyHashable] leads to a compiler warning sayiing that casting String to String always succeed. removing it leads to a compiler error with illegal instruction.
Solution 1:[1]
Hum writing my question lead to me finding the answer.
turns out:
let result: [String: AnyHashable] = [:]
result["key1"] = [...] as? [String: AnyHashable]
casts the right side of the equality that only need to be casted as? AnyHashable for my result array to work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | FitzChill |