[更好地支持 Swift 中的异构容器]
Any
、[Any]
和 [String: Any]
是类型擦除的值,允许存储异构数据结构。 通常,这些擦除的结构很难使用,并且经常出现在使用 JSONSerialization
或 Cocoa API(例如 Bundle 和 UserDefaults)的代码中。 Eumorphic 旨在通过为这些擦除的类型提供轻量级的语法糖来减轻使用负担。
import Eumorphic
var dictionary: [String: Any] = [
"string": "hello world",
"int": 1,
"structure": [
"is": [
"good": [
true,
[
"and": [
"i": [
"like": [
"pie",
"programming",
"dogs"
]
]
]
]
]
]
]
]
dictionary["string"] // "hello world"
dictionary["string"] = "hello swift" // "hello swift"
dictionary["string" as Path] == "hello swift" // true
dictionary[path: "string"] == "hello swift" // true
dictionary["structure", "is", "good", 0] // true
dictionary["structure", "is", "good", 0, as: Bool.self] // true
dictionary["structure", "is", "good", 0] == "no" // false
dictionary["structure.is.good[0]" as Path] // true
dictionary["structure", "is", "good", 1, "and", "i", "like", 3] // nil
dictionary["structure", "is", "good", 1, "and", "i", "like", .last] // dogs
dictionary["structure", "is", "good", 5, "and", "i", "like", 3] = [ "noodles", "chicken" ]
dictionary["structure", "is", "good", 5, "and", "i", "like", 3] // ["noodles", "chicken"]
dictionary["structure", "is", "good", .first] = false
dictionary["structure", "is", "good", .first] = true
dictionary[path: "structure"] = true
dictionary[path: "structure"] // true