kangna sharma
kangna sharma

Reputation: 102

Dictionary Key Present or Not?

Can you please help me to Make an extension in swift of Dictionary to find whether key is present or not? I want to use this extension in through out my program.

Upvotes: 1

Views: 89

Answers (1)

Jaydeep Vora
Jaydeep Vora

Reputation: 6223

you can use this extension of Dictionary to check whether key is exist or not.

extension Dictionary {
    func contain(_ key: Key) -> Bool {
      return self[key] != nil
    }
}

i.e

let dict = ["temp" : 2, "temp2" : false, 501 : "2"] as [AnyHashable : Any]

dict.contain("temp") // true

Upvotes: 2

Related Questions