如何通过随机键值对过滤数组

时间:2018-02-20 16:56:02

标签: swift filter

我有这样的模型

struct World {
    var country: String
    var city: String
}

  var world = [ World(country: "USA", city: "New York"),
                World(country: "USA", city: "California"),
                World(country: "France", city: "Paris"),
                World(country: "Italy", city: "Milano") ]

我想通过类似于["country": "USA", "city": "New York", "country": "France", "city": "Milano"]的字典过滤数组。问题是它始终不在同一个顺序中。所以,下一个字典可能就像["city": "New York", "city": "Milano", "country": "USA"]

我试过这样的。

dictionary.forEach { (arg) in
            let (key, val) = arg
            world.filter({$0.key == val})
}

但错误说

  

类型的价值'世界'没有会员'关键'

我怎样才能做到这一点?提前谢谢!

修改

这是我的整个代码

import UIKit

struct World {
    var country: String
    var city: String
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var tableVIew: UITableView!

    var sectionName = ["country", "city"]
    var array = ["USA", "Italy", "France"]
    var array1 = ["New York", "Milano", "Paris"]
    var world = [ World(country: "USA", city: "New York"),
                  World(country: "USA", city: "California"),
                  World(country: "France", city: "Paris"),
                  World(country: "Italy", city: "Milano") ]

    override func viewDidLoad() {
        super.viewDidLoad()

         tableVIew.delegate = self
         tableVIew.dataSource = self
    }

    @IBAction func done(_ sender: Any) {
        print(gotDic)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionName.count
    }

    func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
        return sectionName[section]
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return array.count
        }
        else if section == 1 {
            return array1.count
        }
        else{
            return 0
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableVIew.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        switch indexPath.section {
        case 0:
            cell.textLabel?.text = array[indexPath.row]
        case 1:
            cell.textLabel?.text = array1[indexPath.row]
        default:
            break
        }
        return cell
    }

    var gotDic = [ String : String ]()

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let index = tableVIew.indexPathForSelectedRow
        let section = indexPath.section
        let currentCell = tableVIew.cellForRow(at: index!)
        let secName = sectionName[section]
        let text = currentCell!.textLabel!.text

        if currentCell?.accessoryType == .checkmark {
            gotDic.removeValue(forKey: text!)
            currentCell?.accessoryType = .none
        } else {
            gotDic.updateValue(secName, forKey: text!)
            currentCell?.accessoryType = .checkmark
        }
        print(gotDic)
    }    
}

2 个答案:

答案 0 :(得分:1)

您的world对象不是字典,而是包含World项的数组。要将world列表过滤为仅包含“美国”国家/地区,您可以执行以下操作:

let val = "USA"
let result = world.filter({$0.country == val})

结果将是

[World(country: "USA", city: "New York"), World(country: "USA", city: "California")]

按城市和国家/地区进行过滤

let country = "USA"
let city = "New York"
let result = world.filter({$0.country == country && $0.city == city})

结果将是

[World(country: "USA", city: "New York")]

答案 1 :(得分:1)

  1. 仅供参考:词典是无序键值关联的集合。每个密钥必须是唯一的
  2. 您的gotDic看起来像["New York": "city", "France": "country", "Milano": "city", "USA": "country"]
  3. 你可能正在寻找......:

    let userSelection = Array(gotDic.keys)
    let result = world.filter {
        userSelection.contains($0.city) && userSelection.contains($0.country)
    }