0
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) ......
       }
    }
}

How can I achieve this?

6
  • 2
    Could you post the error? Commented Jan 25, 2016 at 6:36
  • Indeed... the whole point of the if-let-as? is to attempt something that might fail and not throw an error if it does... Commented Jan 25, 2016 at 6:38
  • Replace Array with [AnyObject] Commented Jan 25, 2016 at 6:38
  • "Ambigous reference to member subscript" Commented Jan 25, 2016 at 6:38
  • Also, perhaps you would like to define your method as func isInsideList(id: Int, whichList: String) -> Bool Commented Jan 25, 2016 at 6:38

2 Answers 2

1

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

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

  if theList.contains(id) {

        return true
    }
}

return false

}

Sign up to request clarification or add additional context in comments.

2 Comments

How do I append to this list? It says "Any?" has no member "append"
var str = "Hello, playground" var lists = [String : AnyObject]() lists["lobby"] = [1,3,4,5,6] lists["events"] = [3,6,78,8] lists["missed"] = [0,9,9,9,9,9] 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? [Int] { //this throws an error if theList.contains(id) { return true } } return false } isInsideList(6, whichList: "lobby")
0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.