LazyFilterBidirectionalCollection<Results<datatype>>' 到预期的参数类型 '[datatype]'

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

在我将我的项目从 swift 2.3 转换为 swift 3 之后,我在 Realm Filter 中遇到了这个错误我无法在数组中获得过滤结果:

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)

}

filtermap等是对objects返回的Results的惰性操作。这是在 LazyFilterBidirectionalCollection<T> 等类型中实现的。要实际执行过滤并获得结果数组,您需要通过包装数组初始值设定项(例如 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.