2017-01-07 90 views
1

我正在使用Swift 3中的filter()方法,但遇到问题,我的代码是....无法将类型'(_) - > Bool'的值转换为期望的参数类型'NSPredicate'

filtered = arrayTag.filter(using: { (text) -> Bool in 
     //Access the title and sectors 
     let tmpTitle = text["tag_name"] as! String 
     let tmpSector = text["tag_id"] as! String 

     //Create a range for both 
     let range1 = tmpTitle.range(of: searchText, options: NSString.CompareOptions.caseInsensitive) 
     let range2 = tmpSector.range(of: searchText, options: NSString.CompareOptions.caseInsensitive) 
     print("search result \(text)") 

     //Return true if either match 
     return range1 != nil || range2 != nil 
    }) 
+1

'arrayTag'是什么类型? – dfri

+0

var arrayTag:NSMutableArray = NSMutableArray() –

+2

'NSMutableArray'不响应'filter'。使用本地Swift'Array'。 – vadian

回答

0

这是上述问题

let tmpTitle = (text as! NSDictionary)["tag_name"] as? String 

铸文字as! NSDictionary的解决方案。

0

方法filter(using:)NSMutableArray对象以NSPredicate为参数。要使用filter功能改变喜欢你的代码:

filtered = arrayTag.filter { text in 
    //your code 
} 
+0

我正在尝试这个,但不为我工作 –

+0

为什么?你有没有错误?或者同样的错误? –

+0

相同的错误 - 无法将类型'(_) - > Bool'的值转换为期望的参数类型'NSPredicate' –

相关问题