无法到达 array->dict->set
Unable to reach array->dict->set
我想在数组中的字典中使用我的集合。但我无法达到它。这是代码。
class data: UIView {
func myFunc() {
var arr: Array<Dictionary<String,Any>> = []
var dict: Dictionary<String,Any> = [:]
dict.updateValue("Title1", forKey: "title")
dict.updateValue("Direction 1", forKey: "directions")
dict.updateValue(Set(["Item1"]), forKey: "items")
arr.append(dict)
dict.updateValue("Title2", forKey: "title")
dict.updateValue("Direction 2", forKey: "directions")
dict.updateValue(Set(["Item2"]), forKey: "items")
arr.append(dict)
当我写
let set1 = arr[0]["items"] as? Set<String> ?? Set<String>()
比我想用这套
data.myFunc.set1
问题是,它只给我带括号的 data.myFunc() 属性。所以我无法进入。我做错了什么。感谢您的回答。
我简化了你的语法并提出了以下内容:
func myFunc() {
var arr = [[String : Any]]()
var dict = [String : Any]()
dict["title"] = "Title1"
dict["directions"] = "Direction 1"
dict["items"] = Set(arrayLiteral:"Item1")
arr.append(dict)
if let set1 = arr[0]["items"] as? Set<String> {
print(set1)
}
}
myFunc()
这个输出是:
["Item1"]
func myFunc() {
var arr = [[String : Any]]()
var dict = [String : Any]()
dict["title"] = "Title1"
dict["directions"] = "Direction 1"
dict["items"] = Set(["Item1","Item2"])
arr.append(dict)
if let set1 = arr[0]["items"] as? Set<String> {
print(set1, set1.dynamicType)
}
}
myFunc()
输出是
["Item2", "Item1"] Set<String>
符合预期...
您不能通过函数访问变量。相反,在这种情况下,我建议您 return 设置或将其存储在您的 class 中以供以后访问:
class Data: UIView {
var set = Set<String>()
func myFunc() -> Set<String> {
...
let set1 = arr[0]["items"] as? Set<String> ?? Set<String>()
// store it
set = set1
// or return it
return set1
}
}
// access
let data = Data()
// fist way
data.myFunc()
let newSet = data.set
// second way
let newSet = data.myFunc()
对于return来说,集合大多是首选方式。
我想在数组中的字典中使用我的集合。但我无法达到它。这是代码。
class data: UIView {
func myFunc() {
var arr: Array<Dictionary<String,Any>> = []
var dict: Dictionary<String,Any> = [:]
dict.updateValue("Title1", forKey: "title")
dict.updateValue("Direction 1", forKey: "directions")
dict.updateValue(Set(["Item1"]), forKey: "items")
arr.append(dict)
dict.updateValue("Title2", forKey: "title")
dict.updateValue("Direction 2", forKey: "directions")
dict.updateValue(Set(["Item2"]), forKey: "items")
arr.append(dict)
当我写
let set1 = arr[0]["items"] as? Set<String> ?? Set<String>()
比我想用这套
data.myFunc.set1
问题是,它只给我带括号的 data.myFunc() 属性。所以我无法进入。我做错了什么。感谢您的回答。
我简化了你的语法并提出了以下内容:
func myFunc() {
var arr = [[String : Any]]()
var dict = [String : Any]()
dict["title"] = "Title1"
dict["directions"] = "Direction 1"
dict["items"] = Set(arrayLiteral:"Item1")
arr.append(dict)
if let set1 = arr[0]["items"] as? Set<String> {
print(set1)
}
}
myFunc()
这个输出是:
["Item1"]
func myFunc() {
var arr = [[String : Any]]()
var dict = [String : Any]()
dict["title"] = "Title1"
dict["directions"] = "Direction 1"
dict["items"] = Set(["Item1","Item2"])
arr.append(dict)
if let set1 = arr[0]["items"] as? Set<String> {
print(set1, set1.dynamicType)
}
}
myFunc()
输出是
["Item2", "Item1"] Set<String>
符合预期...
您不能通过函数访问变量。相反,在这种情况下,我建议您 return 设置或将其存储在您的 class 中以供以后访问:
class Data: UIView {
var set = Set<String>()
func myFunc() -> Set<String> {
...
let set1 = arr[0]["items"] as? Set<String> ?? Set<String>()
// store it
set = set1
// or return it
return set1
}
}
// access
let data = Data()
// fist way
data.myFunc()
let newSet = data.set
// second way
let newSet = data.myFunc()
对于return来说,集合大多是首选方式。