gpt4 book ai didi

swift - 如何在 Swift 中过滤字典数组

转载 作者:行者123 更新时间:2023-11-28 07:39:54 25 4
gpt4 key购买 nike

我选择使用字典对象的可变数组来填充 NSTableview,其中字典键是列标识符,值是列值。我像这样填充数组:

var compArray: NSMutableArray = []

let dict = ["idCompany": id, "company": companyName, "compType":
companyType] as [String : Any]
compArray.add(dict)

id、company 和 compType 来自 SQlite 查询。

我使用 compArray 作为 tableView 数据源。这很好用。不涉及阵列 Controller 。

使用 CDM 加载表如下,CDM 是提供 compArray 的类的实例

//Define the function that will get the data for each cell for each 
//row.
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {

//Define constant dict as an NSDictionary and set to the DM
//instance of the DataModel class at the row being loaded into the
//table. DM needs to be cast as an NSDictionary. row is passed in as a
//parameter by the OS
let tdict:NSDictionary = CDM.compArray[row] as! NSDictionary

//Define strKey as the column identifier for the column being
//loaded. Column being loaded is passed in as a parameter by the OS
let strKey = (tableColumn?.identifier)!

//method will return the value from dict (which is loaded from
//CDM.compArray) for the key that is equal to the column identifier
//which was loaded to strKey
return tdict.value(forKey: strKey.rawValue)
}

我想做的是引入一个 NSSearch 字段来搜索 tableview 的所有列。

我添加了搜索字段作为一个 Action ,我还添加了代码以将 compArray 存储到一个名为 backupCompArray 的副本中。

我定义了一个 isSearching 变量:

//The variable is set to true when searching
var isSearching = false {
//This will be fired if the state of isSearching changes ie false
//to true or true to false
didSet {

//Test to see whether isSearching now has a new value and if
//so we do the housekeeping
if isSearching != oldValue {

//If isSearching is now true then it must have previously
//been false and we now need to back up the original array
if isSearching {
//Back up the original array
backUpCompArray = compArray
} else{
//It is now turning from true to false so need to
//restore
compArray = backUpCompArray
}
}
}
}

我希望能够根据搜索字段的 .stringValue 过滤 compArray。

我已将以下内容放入搜索字段的@IBAction 中:

@IBAction func searchField(_ sender: NSSearchFieldCell) {
if sender.stringValue.isEmpty {
//If stringValue is empty then cant be searching and this can
//trigger the restore of the original array
isSearching = false
} else {

//If stringValue is not empty then must be searching. When the
//search starts need to backup the original array
isSearching = true

//?????????????
}
}

tableView.reloadData()
}

我需要更换 ?使用可以通过将过滤器应用于 backupCompArray 来设置 compArray 的代码,将任何行返回到 compArray,其中键“company”和“compType”的字典值包含 searchfield.Stringvalue。然后,我可以使用修改后的 compArray 加载仅包含过滤行的表。

所以我尝试了您的代码并遇到了两个错误,我尝试按如下方式修复:

//Can use the .filter method it will iterate each value in the array
CDM.compArray = backupCompArray.filter(using: {
// this is where you determine whether to include the
specific element, $0
$0["company"]!.contains(sender.stringValue) &&
$0["compType"]!.contains(sender.stringValue)
// or whatever search method you're using instead
})
}

那是插入 'using' 并将 searchString 更改为 sender.Stringvalue。

但我现在得到:enter image description here

针对以&&结尾的行

最佳答案

您可以使用 Swift 4 的 Array.filter() 来做到这一点。它需要一个函数,您可以在其中进行计算。您实际上不需要备份您的 compArray,除非您以后需要保留它以备不时之需。

compArray = compArray.filter({
// this is where you determine whether to include the specific element, $0
$0["company"]!.contains(searchString) &&
$0["compType"]!.contains(searchString)
// or whatever search method you're using instead
})

关于swift - 如何在 Swift 中过滤字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52570745/

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