Jargen89
Jargen89

Reputation: 480

How to I look through an array that exists in a dictionary in Swift?

I am trying to loop through array that exists in a dictionary. What am I doing wrong and how should this work?

    public func getItemCount() -> Int{

        var count = 0
        for item in order.object(forKey:"items") as? Array {
            count += 1
            //other instructions
        }
        return count
    }

Upvotes: 1

Views: 57

Answers (1)

KrishnaCA
KrishnaCA

Reputation: 5695

Try breaking it down to two steps.

public func getItemCount() -> Int{

    if let thisArray:[AnyObject] = order.object(forKey:"items") as? [AnyObject] {
       for item in thisArray {

       }
    }
}

Feel free to suggest edits. Please let me know if it doesn't work

Upvotes: 3

Related Questions