gpt4 book ai didi

ios - 如何过滤包含字符串和数组的结构?

转载 作者:行者123 更新时间:2023-11-30 12:31:14 24 4
gpt4 key购买 nike

基本上,这是一个包含国家及其端口的结构对象

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

这些是保存每个国家/地区映射到其端口的数组。过滤后的数组应保存按国家/地区或端口过滤的结果。

var countryAndPortsArray = [countryAndPorts]()
var filteredArray = [countryAndPorts]()

目前,以下函数成功生成使用国家/地区作为搜索键的过滤结果

func filteredContent(searchKey: String) {
filteredArray = countryAndPortsArray.flatMap{ $0 }.filter{$0.country?.lowercased().range(of: searchKey) != nil }
}

现在我的问题是我希望通过使用端口或国家/地区作为搜索键来获得过滤结果。我已尽力而为,但使用端口作为搜索键似乎无法获得理想的结果。对我当前方法的改进将不胜感激。 Objective-C 中的答案也受到欢迎。

//Just incase it's of any use, these are my UITableView delegates 
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
}

-------------------------------- UDPATE ----------- --------------------

目前,输入“vlone”作为搜索字符串会返回“vlone”和“vlore”。 Vlone 和 Vlore 都是阿尔巴尼亚的港口

Result yielded using vlone as search string

但是,所需的输出应仅包含“vlone”,而忽略阿尔巴尼亚的所有其他端口,因为用户的查询是特定的。

第二种情况是使用“barcelona”作为搜索键。它返回包含西类牙每个港口的结果,但所需的输出应仅包含巴塞罗那。

Results using barcelona as search key

最佳答案

如果您想过滤数组以查找具有匹配国家/地区或端口的任何条目,那么您可以执行以下操作:

func filteredContent(searchKey: String) {
filteredArray = countryAndPortsArray.filter {
$0.country?.lowercased().range(of: searchKey) != nil ||
!($0.ports?.filter {
$0.lowercased().range(of: searchKey) != nil
}.isEmpty ?? true)
}
}

让我们来分解一下。顶层是对 countryAndPortsArray 数组上的 filter 的调用。过滤器由两部分组成。 1) 检查国家/地区是否包含搜索文本,2) 检查是否有任何端口包含搜索文本。

第一次检查是通过以下方式完成的:

$0.country?.lowercased().range(of: searchKey) != nil

这是您已经拥有的部分。

第二次检查完成:

!($0.ports?.filter {
$0.lowercased().range(of: searchKey) != nil
}.isEmpty ?? true)

由于 ports 是一个数组,因此使用过滤器来查看是否有任何端口包含搜索文本。 isEmpty 正在检查匹配端口的结果过滤器列表是否为空。由于 ports 是可选的,因此使用 ?? true 表示将匹配端口列表视为空。 ! 否定结果。因此,如果匹配列表为空(或 portsnil),则 true 被否定为 false 表明没有匹配的端口。

这两项检查使用标准逻辑“或”运算符 || 进行组合,这意味着只需两项检查之一为真,顶级过滤器即可匹配该记录。

关于ios - 如何过滤包含字符串和数组的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com