Danil Onishenko
Danil Onishenko

Reputation: 55

How to check if value is in dictionary?

I'm using Alamofire and SwiftyJSON and I want to check if the response contains a value that I will type in a search bar.

I've just got the whole JSON file of 1666 objects and append it into my objects array and then I'm searching for value, but it takes too long.

func parseJSON(json: JSON, parse: ParseType) {
  var i = 0
  var j = 0
  switch parse {
    case .group:
      for elements in json["groups"] {
        if let groupId = json["groups"][i]["id"].int {
          let groupName = json["groups"][i]["name"].string
          // print(groupId)
          let group = Groups(id: groupId, name: groupName!)

          groupsArray.append(group)
          i += 1
        } else {
          print("Error can't parse JSON")
        }
      }

      func getGroupsData(url: String, groupName: String) {
      Alamofire.request(url, method: .get).responseJSON { (response) in
        if response.result.isSuccess {
          print("Is Success")
          let json = JSON(response.result.value)
          self.parseJSON(json: json, parse: .group)
          if let group = self.groupsArray.first(where: {$0.name == groupName}) {
            print("found \(group)")
            let searchScheduleUrl = url + "\(group.id)"
            self.getGroupSchedule(url: searchScheduleUrl)
          } else {
            print("Can't find group")
          }
        } else {
          print(response.result.error)
        }
      }
    } 

And here is JSON:

{
 "groups" : [
   {
    "faculty" : {
    "id" : 101,
    "abbr" : "ИнГО",
    "name" : "Гуманитарный институт"
  },
  "id" : 26262,
  "spec" : "47.06.01 Философия, этика и религиоведение",
  "kind" : 3,
  "level" : 3,
  "name" : "33865\/4702",
  "type" : "common"
},
{
  "faculty" : {
    "id" : 95,
    "abbr" : "ИКНТ",
    "name" : "Институт компьютерных наук и технологий"
  },
  "id" : 27432,
  "spec" : "09.03.04 Программная инженерия",
  "kind" : 0,
  "level" : 1,
  "name" : "в13534\/22",
  "type" : "evening"
},
{
  "faculty" : {
    "id" : 92,
    "abbr" : "ИСИ",
    "name" : "Инженерно-строительный институт"
  },
  "id" : 26322,
  "spec" : "08.06.01 Техника и технологии строительства",
  "kind" : 3,
  "level" : 1,
  "name" : "13163\/0801",
  "type" : "common"
}, and so on...

I want to check for example:
if name: "13541/1" is in dictionary and if it is i want to get it's id

Upvotes: 0

Views: 60

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can try

struct Root: Codable {
    let groups: [Group]
}

struct Group: Codable {
    let faculty: Faculty
    let id: Int
    let spec: String
    let kind, level: Int
    let name, type: String
}

struct Faculty: Codable {
    let id: Int
    let abbr, name: String
}

do {
    let res = try JSONDecoder().decode(Root.self,from:data) 
    if let item = res.groups.first(where:{  $0.name == YourName }) {
      print(item.id)
    }
}
catch {
  print(error)
}

Upvotes: 2

Related Questions