搜索栏在 swift 3 中不起作用。无法将类型 (_) -> Bool 的值转换为预期的参数类型 NSPredicate

Posted

技术标签:

【中文标题】搜索栏在 swift 3 中不起作用。无法将类型 (_) -> Bool 的值转换为预期的参数类型 NSPredicate【英文标题】:Search bar is not working in swift 3. Cannot convert value of type (_) -> Bool to expected argument type NSPredicate 【发布时间】:2017-04-28 12:00:35 【问题描述】:

不断显示错误:无法将类型 (_) 的值 -> Bool 转换为预期的参数类型 NSPredicate 上线:

filtered = OrdersArray.filter(using:  (text) -> Bool in
            let tmp: NSString = text
            let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) 

        filtered = OrdersArray.filter(using:  (text) -> Bool in
            let tmp: NSString = text
            let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
            return range.location != NSNotFound
        )

        if(filtered.count == 0)
            searchActive = false;
         else 
            searchActive = true;
        
        self.tableView.reloadData()
    

UISearchBarDelegateUISearchDisplayDelegate 代表都被标记 searchBarTextDidBeginEditing, searchBarTextDidEndEditing, searchBarCancelButtonClicked, searchBarSearchButtonClicked 函数写成 请告诉我在NSMutableArray 中搜索数据的其他方法。

这是我的数组。


orders =     (
            
        "created_at" = "2017-05-01 04:49:17";
        id = 591;
        "order_status" = CONFIRMED;
        "pickup_code" = 4356219;
        store =             
            address1 = "**********";
            city = "**********";
            description = "*************";
            logo = "fghdshdx.png";
            name = "*******";
            "postal_code" = 110006;
            storeTotal = "20.00";
            "store_id" = 4;
            "tin_number" = M1O2R3E4MORE;
        ;
        total = "20.00";
        "updated_at" = "2017-05-01 04:49:20";
        "user_address_id" = "<null>";
        "user_id" = 5372;
        "user_name" = "Amit Sinha";
    
);
status = 1;

【问题讨论】:

filtered 数组的类型是什么,OrdersArray 也包含字符串作为对象? @NiravD var OrdersArray = NSMutableArray(), var filters:[String] = [] @RajatAttri 像这样你必须做 var OrdersArray = [String]() 和 var filters:[String] = [] 并且在视图中确实加载了你可以分配 OrdersArray = ["San Francisco", "纽约","圣何塞","芝加哥","洛杉矶","奥斯汀","西雅图"] @RajatAttri Sweeper 已发布解决方案。 @RajatAttri 试试这个: let namePredicate = NSPredicate(format: "yourkey %@","searched text");// 用 yourkey 替换你的密钥 let filters = (self.arrQuestions as NSArray).过滤(使用:namePredicate) 【参考方案1】:

不要使用NSArray

我猜你的OrdersArrayNSArrayNSMutableArray。您应该先将其转换为 swift 数组:

let swiftArray = Array(OrdersArray).map   $0 as! NSString 

然后过滤swift数组:

filtered = swiftArray.filter(using:  (text) -> Bool in
    let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
    return range.location != NSNotFound
)

【讨论】:

无法将“__NSDictionaryM”(0x1a7a25c50)类型的值转换为“NSString”(0x1a7a35ae8)。 2017-05-01 10:31:03.423309+0530 UniShop Merchant [3029:1014358] 无法将“__NSDictionaryM”(0x1a7a25c50)类型的值转换为“NSString”(0x1a7a35ae8)。【参考方案2】:

试试这个!来自This的参考

  import UIKit

class SearchViewController: UIViewController,UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate 


    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var SearchTableView: UITableView!

    var searchActive : Bool = false
    var data = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"]
    var filtered:[String] = []

    override func viewDidLoad() 
        super.viewDidLoad()

        /* Setup delegates */
        SearchTableView.delegate = self
        SearchTableView.dataSource = self
        searchBar.delegate = self
    
    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()

    

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) 
        searchActive = true;
    

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) 
        searchActive = false;
    

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) 
        searchActive = false;
    

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) 
        searchActive = false;
    

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) 

        filtered = data.filter( (text) -> Bool in
            let tmp: NSString = text as NSString
            let range = tmp.range(of: searchText, options: .caseInsensitive)
            return range.location != NSNotFound
        )
        if(filtered.count == 0)
            searchActive = false;
         else 
            searchActive = true;
        
        SearchTableView.reloadData()
    



    func numberOfSectionsInTableView(tableView: UITableView) -> Int 
        return 1
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        if(searchActive) 
            return filtered.count
        
        return data.count;
    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! as UITableViewCell;
        if(searchActive)
            cell.textLabel?.text = filtered[indexPath.row]
         else 
            cell.textLabel?.text = data[indexPath.row];
        

        return cell;
    

【讨论】:

我看过这段代码:它属于achrak,我只是从那里复制的,它不起作用 @RajatAttri 它对我有用,我在 swift3 中更新代码 NSMutableArray = OrderArray, Filtered 和 urs 一样!还是不行 你能告诉我你如何声明和初始化数组吗

以上是关于搜索栏在 swift 3 中不起作用。无法将类型 (_) -> Bool 的值转换为预期的参数类型 NSPredicate的主要内容,如果未能解决你的问题,请参考以下文章

OSX 系统菜单栏在 JavaFX 中不起作用

UITapGestureRecognizer 在 Swift 3.0 中不起作用

为啥类型转换在 Swift 2.0 中不起作用?

枚举中的类型转换在 Swift 中不起作用

jquery 和 bootstrap 导航栏在 Angular 7 中折叠或展开子菜单的布局中不起作用

Swift UrlSession 在 UrlSession 中不起作用