Search code examples
arraysswiftswift-dictionary

extend an Array of Dictionary<String, Any> Swift 3


var dicts = [["key1": "value1", "key2": "value2"]]

dicts.values(of: "key1") // prints - value1

I am working on a project where I want to store the array of dictionary and then fetch the data from there on condition if array of dictionary contains the particular value.


Solution

  • Swift 3.0

    You can try this way.

    var dicts:[[String:Any]] = []
    var check:Bool = false
    
    dicts = [["search_date": "17/03/17", "search_title": ""],["search_date": "17/02/19", "search_title": "parth"],["search_date": "20/02/19", "search_title": "roy"]]
    
    for item in dicts {
        if let title = item["search_title"] as? String {
            if title == "parth"  {
                check = true
                break
            }else {
                check = false
            }
        }
        else {
           check = false
        }
        
    }
    print(check)