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

标签 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})
}

但是错误提示

Value of type 'World' has no member 'key'

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

编辑

这是我的全部代码

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)
    }    
}

最佳答案

您的 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")]

关于swift - 如何通过随机键值对过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48890386/

相关文章:

javascript - 如何获取具有相同值的两个对象,并使它们被视为一个对象

ios - Swift:REST JSON Get 请求带身份验证

swift - NSDictionary 在检索信息时返回一个可选的整数

spring - Spring Security Filter Chain 如何工作

list - Prolog - 过滤器列表

xcode - 从 NSArray 中删除不需要的数据

javascript - 使用 JS 的多重过滤表

swift - 注册/登录 swift 2.0 错误

ios - 我的 UIStackView 框架彼此堆叠

swift - 从特定点缩放 SpriteNode