如何在Swift中过滤字典数组

时间:2018-09-29 17:17:43

标签: swift nsmutablearray nsdictionary nssearchfield

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

var compArray: NSMutableArray = []

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

id,company和compType来自SQlite查询。

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

使用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字段来搜索表视图的所有列。

我添加了搜索字段作为操作,还添加了将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

以&&

结尾的行

1 个答案:

答案 0 :(得分:0)

您可以使用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
})