If you can opt to typecast the dictionary, do so, like the following:
var lists = Dictionary<String, [Int]>()
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] {
for (var i = 0; i < theList.count; i++){
if (id == theList[i]){
return true
}
}
}
return false
}
However, if you require that your dictionary contains an array of possibly various kinds of objects, then you can do the following:
var anyList = Dictionary<String, Array<AnyObject?>>()
anyList["lobby"] = [String]()
anyList["events"] = [String]()
anyList["missed"] = [String]()
func isInsideAnyList(id: Int, whichList: String) -> Bool{
// Attempt to get the list, if it exists
if let theList = anyList[whichList] {
// Loop through each element of the list
for (var i = 0; i < theList.count; i++){
// Perform type cast checks on each element, not on the array itself
if (String(id) == theList[i] as? String){
return true
}
}
}
return false
}
if-let-as?is to attempt something that might fail and not throw an error if it does...func isInsideList(id: Int, whichList: String) -> Bool