和自定义对象数组

和自定义对象数组

本文介绍了Swift 4:来自 UITextField 的 NSPredicate 和自定义对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组自定义对象.

class MyObject {
    var code: String!
    var name: String!
}

我想autocomplete一个textField,因为我有一个tableView,当用户开始写作时我会显示它.我必须根据 textField.text 过滤我的数组数据源.为此,我向 textField 添加了一个选择器,然后我使用 NSPredicate 测试包含 textFiled.text 的数组中是否有元素的名称.

I want to autocomplete a textField for that I have a tableView that I display when the user start writing. I have to filter my array datasource depending on the textField.text. For that I added a selector to the textField, then I test if there are names of elements in the array containing the textFiled.text using NSPredicate.

这是 textField 选择器:

This is the textField selector:

    @objc func textFieldDidChange(_ textField: UITextField) {
            if (textField.text?.count != 0 && textField.text != " ") {
                let resultPredicate = NSPredicate(format: "ANY name CONTAINS[c] %@", self.countryTextField.text!)
                print("resultPredicate \(resultPredicate)")
//self.allDatasource type is [MyObject]
                self.filtredDatasource = self.allDatasource.filter({
                    return resultPredicate.evaluate(with: $0.name)
                })
            } else {
                self.filtredDatasource = self.allDatasource
            }
            self.tableView.reloadData()
        }

当我开始写名字时,应用程序因为这个错误而崩溃:

When I start writing a name the application crash due to this error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x6040004215e0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'

所以问题出在NSPredicate 格式.我搜索了这个问题,我发现我可以使用 ANY 然后输入我想检查的字段名称.

So the problem is in NSPredicate format. I searched about this issu and I found that I can use ANY then put the field name that I want to check.

我错了吗?我该如何解决这个问题?

Am I wrong? How can I solve this problem?

注意:我使用的是 Swift 4

Note: I'm using Swift 4

推荐答案

我认为要使其正常工作,您需要使您的对象可用于 Objective-C 端:

I think for this to properly work, you need to make your object available to the Objective-C side:

class MyObject: NSObject {
    init(code: String, name: String) {
        self.code = code
        self.name = name
    }

    var code: String
    @objc var name: String
}

谓词在你给它的对象上进行评估.在您的情况下,您为其提供一个 String.对于要检查的谓词,您应该使用 self:

The predicate is evaluated over the object you give it. In your case, you provide it a String. For the predicate to check that, you should use self:

let resultPredicate = NSPredicate(format: "self contains[cd] %@", searchText)

let filtered = allDatasource.filter {
    resultPredicate.evaluate(with: $0.name)
}

你也可以给它一个MyObject来求值,然后你就可以使用属性的名字了:

You can also give it a MyObject to evaluate, then you can use the name of the property:

let resultPredicate = NSPredicate(format: "name contains[cd] %@", searchText)

let filtered = allDatasource.filter {
    resultPredicate.evaluate(with: $0)
}

在您的代码中,您使用 ANY 修饰符.这是一个修饰符,您可以用来检查集合中是否有任何 项符合谓词:

In your code, you use the ANY modifier. That's a modifier you can use to check if there is any item in a collection that adheres to the predicate:

let searchText = "n2"
let resultPredicate = NSPredicate(format: "ANY name contains[cd] %@", searchText)

let allDatasource = [ MyObject(code: "c1", name: "n1"), MyObject(code: "c2", name: "n2")]
let containsAnyObject = resultPredicate.evaluate(with: allDatasource)

// true for 'n2'

这篇关于Swift 4:来自 UITextField 的 NSPredicate 和自定义对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:31