LazyFilterBidirectionalCollection<Results<datatype>>' to expected argument type '[datatype]'

让人想犯罪 __ 提交于 2019-12-13 03:46:34

问题


After I convert my project from swift 2.3 to swift 3 , I got this error in Realm Filter I can't get filter result in array :

func filterUsers(_ searchText: String, completion: (([Lawyer]) -> Void)) {

        let bgRealm = try! Realm()            // Filter the whole list of users



let results = bgRealm.objects(Lawyer.self).filter { (cc) -> Bool in
    cc.name.contains2(searchText) ||
        cc.name.contains2(searchText) ||
        cc.phoneNumber2.contains2(searchText)

}
    print(results)


    completion(results)

}

回答1:


filter, map, etc. are lazy operations on the Results returned by objects. This is implemented in types such as LazyFilterBidirectionalCollection<T>. To actually perform the filtering and get an array of results, you need to promote this collection to an array by wrapping in an Array initializer (e.g. Array(bgRealm.objects...))

From the docs:

Queries return a Results instance, which contains a collection of Objects. Results have an interface very similar to Array and objects contained in a Results can be accessed using indexed subscripting. Unlike Arrays, Results only hold Objects of a single subclass type. All queries (including queries and property access) are lazy in Realm. Data is only read when the properties are accessed.



来源:https://stackoverflow.com/questions/45083630/lazyfilterbidirectionalcollectionresultsdatatype-to-expected-argument-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!