【问题标题】:Filtering a struct object containing a string and array of strings过滤包含字符串和字符串数组的结构对象
【发布时间】:2017-04-26 09:58:30
【问题描述】:

我有一个结构对象,其中包含端口到各自国家/地区的映射。

struct countryAndPorts {
    var country: String?
    var ports: [String]?
}

我有一个数组 var countryAndPortsArray = [countryAndPorts]() 包含每个国家/地区的 countryAndPort 对象。

我还有一个数组 var filteredArray = [countryAndPorts](),其中包含根据用户的搜索查询过滤的 countryAndPortsArray 对象

我正在用这个函数过滤countryAndPortsArray

func filteredContent(searchKey: String) {
    filteredArray = countryAndPortsArray.filter {
      !($0.ports?.filter{
           $0.range(of: searchKey, options: .caseInsensitive, range: 
           nil, locale: nil) != nil || 
           $0.localizedCaseInsensitiveCompare(searchKey) == .orderedSame
          }.isEmpty ?? true) || 
       $0.country?.localizedCaseInsensitiveContains(searchKey) ?? true
}

我的问题是filteredArray 的结果包含同一国家/地区内的每个端口,而不管用户的搜索关键字是否匹配特定端口。

例如,如果用户搜索“Paris”,filteredArray 将包含法国的每个港口。如果用户搜索“barcelona”,filteredArray 将包含西班牙的每个港口。

所需结果应仅包含与“barcelona”或“paris”匹配的端口,而不应包含同一国家/地区的所有其他端口。

更新

用例:

var france = countryAndPorts(country: "France", ports: 
["monaco","paris","ajaccio","lyon"])

var spain = countryAndPorts(country: "Spain", ports: ["barcelona", 
"madrid", "catalan"])

var countryAndPortsArray = [france,spain]

如果我在countryAndPortsArray 搜索巴塞罗那,它会返回["barcelona", "madrid", "catalan"]。但是我希望它只返回["barcelona"]

这是我的 tableview 代表,以防它为我的问题提供更多信息

extension SearchDealsViewController: UITableViewDelegate, 
UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: 
Int) -> Int {
    if let count = filteredArray[section].ports?.count {
        return count
    }
        return 0
}

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

    if let port = filteredArray[indexPath.section].ports?
[indexPath.row] {
        cell.textLabel?.text = port
    }
    cell.textLabel?.font = UIFont.systemFont(ofSize: 10)
    return cell
}

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: 
IndexPath) {
    portsFromTextField.text = filteredArray[indexPath.section].ports?
    [indexPath.row]
}

func tableView(_ tableView: UITableView, viewForHeaderInSection 
section: Int) -> UIView? {
    let header = tableView.dequeueReusableCell(withIdentifier: 
"header")
    let tapSectionHeaderGesture = UITapGestureRecognizer(target: self, 
    action: #selector(getCountryText(sender:)))
    tapSectionHeaderGesture.numberOfTouchesRequired = 1
    tapSectionHeaderGesture.numberOfTapsRequired = 1
    header?.addGestureRecognizer(tapSectionHeaderGesture)

if header != nil {
    header?.textLabel?.textColor = THEME_COLOUR
    header?.textLabel?.font = UIFont.boldSystemFont(ofSize: 12)
    header?.textLabel?.text = filteredArray[section].country
}

return header
}

func getCountryText(sender: UITapGestureRecognizer) {
    if let country = sender.view as? UITableViewCell {
        portsFromTextField.text = country.textLabel?.text
    }
}

func tableView(_ tableView: UITableView, heightForHeaderInSection 
section: Int) -> CGFloat {
    return 30
}

【问题讨论】:

  • 您能否提供具有预期结果的小数据示例。
  • @OlegGordiichuk 已更新

标签: arrays swift struct


【解决方案1】:

我写了解决方案,但它并不完美。但它仍然返回预期的结果。

首先,我们可以编辑结构以获取首选结果。

struct countryAndPorts {
    var country: String?
    var ports: [String]?

    func getPort(_ withName: String) -> [String]? {
        return ports?.filter{$0.lowercased() == withName.lowercased()}
    }
}

Than out 函数过滤数据。它并不完美,但它可以减少flatMap 调用的数量。

func fetch(port withName: String, from ports: [countryAndPorts]) -> [String]? {
    return ports.map{$0.getPort(withName)}.flatMap{$0}.filter{($0?.count)! > 0}.flatMap{$0}.first
}

结果:

fetch(port: "madrid", from: countryAndPortsArray) // ["madrid"]?

【讨论】:

  • 您好,感谢您的回答。虽然我已经设法编写了一个完美运行的函数,但我仍然会测试你的答案并回复你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 2016-10-18
  • 2019-04-04
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多