gpt4 book ai didi

ios - 如何创建一个以数组为对象的字典?

转载 作者:行者123 更新时间:2023-11-29 01:17:28 24 4
gpt4 key购买 nike

class MyClass {

var lists = Dictionary<String, Any>()

init(){
lists["lobby"] = [Int]()
lists["events"] = [Int]()
lists["missed"] = [Int]()
}


func isInsideList(id: Int, whichList: String) -> Bool{ //whichList could be "lobby", "events", or "missed"
//check if "id" is inside the specified array?
if let theList = lists[whichList] as? Array { //this throws an error
if theList.contains(id) ......
}
}
}

我怎样才能做到这一点?

最佳答案

func isInsideList(id: Int, whichList: String) -> bool {

if let theList = lists[whichList] as? [Int] {  

if theList.contains(id) {

return true
}
}

return false

关于ios - 如何创建一个以数组为对象的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34986433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com