繁体   English   中英

实例方法“包含”要求“UITextField”符合“StringProtocol”

[英]Instance method 'contains' requires that 'UITextField' conform to 'StringProtocol'

我有一个表格视图,它显示一个具有名称和类别属性的 sourceArray。 我想根据来自 UIAlertController 的用户输入过滤 sourceArray 的 Category 属性,但收到错误“Instance method 'contains' requires that 'UITextField' conform to 'StringProtocol'”。 任何帮助是极大的赞赏。

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)
    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM