iOS Swift How to check Key Availability in NSMutab

2019-09-17 14:48发布

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条回答
小情绪 Triste *
2楼-- · 2019-09-17 15:12

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)
}
查看更多
登录 后发表回答