iOS Swift How to check Key Availability in NSMutab

2019-09-17 14:51发布

问题:

I am having some set of NSMutableArray like:

(
        {
        name = "ILTB";
        source = "iltb.net";
    },
        {
        name = "Baran Bench";
        source = "baranbench.com";
    },
        {
        name = "Income Tax India 1";
        source = "Income Tax India 1";
    }
)

How to check Key availability in this NSMutableArray. For Example I need to check "ILTB" is already in my MutableArray or not.

回答1:

First of all ILTB is not key but its value for key name.

You probably want search from array. Now in Swift instead of NSArray use Swift's native array of dictionary and then use first(where:) this way.

let array = [[String:String]]()
if let res = array.first(where: { $0["name"] == "ILTB" }) {
    print(res)
}