Arrays 实例方法&x27;包含';要求';UITextField';符合';StringProtocol';
我有一个tableview,它显示一个带有名称和类别属性的sourceArray。我想根据用户从UIAlertController输入的内容筛选sourceArray的Category属性,但获取错误“Instance method”contains需要“UITextField”符合“StringProtocol”。非常感谢您的帮助Arrays 实例方法&x27;包含';要求';UITextField';符合';StringProtocol';,arrays,swift,uialertcontroller,Arrays,Swift,Uialertcontroller,我有一个tableview,它显示一个带有名称和类别属性的sourceArray。我想根据用户从UIAlertController输入的内容筛选sourceArray的Category属性,但获取错误“Instance method”contains需要“UITextField”符合“StringProtocol”。非常感谢您的帮助 var sourceArray = [(Name: String, Category: String, Picture1: UIImage, Picture2: U
var sourceArray = [(Name: String, Category: String, Picture1: UIImage, Picture2: UIImage, Picture3: UIImage, Description: String)]()
@IBAction func filterButton(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Category", message: nil, preferredStyle: .alert)
let action = UIAlertAction(title: "Filter", style: .default) { (action) in
print(textField)
let filtered = sourceArray.filter({$0.Category.contains(textField)})
self.filteredArray = filteredArray.isEmpty ? sourceArray : filtered
self.tableview.reloadData()
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Biceps, Chest, Shoulders, etc."
textField = alertTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
我遇到的问题是textField没有被识别为字符串。因此,我添加了一个常量文本作为字符串,并将textField.text这样分配给它
@IBAction func filterButton(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Category", message: nil, preferredStyle: .alert)
let action = UIAlertAction(title: "Filter", style: .default) { (action) in
let text: String = textField.text!
let filtered = self.sourceArray.filter({$0.Category.contains(text)})
self.filteredArray = self.filteredArray.isEmpty ? self.sourceArray : filtered
self.tableview.reloadData()
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Biceps, Chest, Shoulders, etc."
textField = alertTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
我遇到的问题是textField没有被识别为字符串。因此,我添加了一个常量文本作为字符串,并将textField.text这样分配给它
@IBAction func filterButton(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Category", message: nil, preferredStyle: .alert)
let action = UIAlertAction(title: "Filter", style: .default) { (action) in
let text: String = textField.text!
let filtered = self.sourceArray.filter({$0.Category.contains(text)})
self.filteredArray = self.filteredArray.isEmpty ? self.sourceArray : filtered
self.tableview.reloadData()
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Biceps, Chest, Shoulders, etc."
textField = alertTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
什么是
sourceArray
?sourceArray是我用来填充tableView的数组。它包含练习名称及其类别。您的代码毫无意义。您创建了一个新的未连接任何东西的UITextField,然后尝试查看sourceArray是否包含该文本字段对象?代码是否创建了textField并创建了UIAlertAction以从sourceArray中筛选文本字段?这就是我对它的理解,很可能我的逻辑在这里不起作用。我正在尝试根据文本字段中输入的内容过滤sourceArray。有更好的方法吗?什么是sourceArray
?sourceArray是我用来填充tableView的数组。它包含练习名称及其类别。您的代码毫无意义。您创建了一个新的未连接任何东西的UITextField,然后尝试查看sourceArray是否包含该文本字段对象?代码是否创建了textField并创建了UIAlertAction以从sourceArray中筛选文本字段?这就是我对它的理解,很可能我的逻辑在这里不起作用。我正在尝试根据文本字段中输入的内容过滤sourceArray。有更好的方法吗?