-2
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.

Hamish
  • 78,605
  • 19
  • 187
  • 280
Parth Dhorda
  • 712
  • 1
  • 8
  • 32

3 Answers3

1

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)
Community
  • 1
  • 1
Hitesh
  • 896
  • 1
  • 9
  • 22
1

We can Use Model to solve the Problem

class Person: NSObject, NSCoding {
let name: String
let age: Int
init(name: String, age: Int) {
    self.name = name
    self.age = age
}
required init(coder decoder: NSCoder) {
    self.name = decoder.decodeObject(forKey: "name") as? String ?? ""
    self.age = decoder.decodeInteger(forKey: "age")
}

func encode(with coder: NSCoder) {
    coder.encode(name, forKey: "name")
    coder.encode(age, forKey: "age")
}

}

Class

class ViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    // setting a value for a key
    let newPerson = Person(name: "Joe", age: 10)
    var people = [Person]()
    people.append(newPerson)
    let encodedData = NSKeyedArchiver.archivedData(withRootObject: people)
    UserDefaults.standard.set(encodedData, forKey: "people")

    // retrieving a value for a key
    if let data = UserDefaults.standard.data(forKey: "people"),
        let myPeopleList = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Person] {
        myPeopleList.forEach({print( $0.name, $0.age)})  // Joe 10
    } else {
        print("There is an issue")
    }
}

}

All Thanks to Leo Dabus

[Link] (https://stackoverflow.com/a/37983027/3706845)

Parth Dhorda
  • 712
  • 1
  • 8
  • 32
0

Your question is very vague. But what I understood is that you want to filter the array of dictionaries so it only contains dictionaries that have a certain value, and this can be done this way:

let filteredDicts = dicts.filter({ $0.values.contains("value2") })
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • Can you tell me how can I define this array globally means like the syntax without the actual values inside the array when app starts Thanks – Parth Dhorda Jul 22 '17 at 10:57
  • var dicts:[[String:Any]] = [], like this – Hitesh Jul 22 '17 at 11:00
  • I have the data inside the array which is dynamic comes from the server [Expample Link] (https://drive.google.com/file/d/0Bzyy3BQ5zuwfMXJNRThVelNRa2c/view?usp=sharing) and from there I wants to search for "Parth" inside search_title if yes then do this else do something else – Parth Dhorda Jul 22 '17 at 11:11