2

I have a declaration..

var cabResults = Dictionary<CabType, [CabResult]>()

Now I want to check if the object is present in the dictionary for a particular key.. which can be don by

if self.cabResults[currentCabType] != nil

Now I also want to check if the object returned by self.cabResults[currentCabType] is of type [CabResult]

How can I get this..?

1
  • if varName is Array<CabResult>{...} Commented Feb 10, 2016 at 11:41

2 Answers 2

3

I would use if let ... as? ...:

if let cabs = self.cabResults[currentCabType] as? [CabResult] {
    // yep
} else {
    // nope
}
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to check is the object is will and do another check is that of any particular type, you can just do:

if let myObject = self.cabResults[currentCabType] as? [CabResult] {
    // myObject is not till and is of type myObject
} else {
    // the object is nil or it's not of type myObject
}

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.