LazyFilterBidirectionalCollection< Results< datatype>>'到预期的参数类型"[数据类型]" [英] LazyFilterBidirectionalCollection<Results<datatype>>' to expected argument type '[datatype]'
问题描述
将项目从swift 2.3转换为swift 3之后,我在Realm Filter中遇到此错误,但无法在array中获得过滤结果:
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)
}
推荐答案
filter
,map
等是对objects
返回的Results
的惰性操作.这以诸如LazyFilterBidirectionalCollection<T>
的类型实现.要实际执行过滤并获取结果数组,您需要通过包装Array初始化器(例如Array(bgRealm.objects...))
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...))
查询返回一个Results实例,其中包含以下内容的集合 对象.结果具有与Array和对象非常相似的接口 可以使用索引下标访问结果中包含的内容. 与数组不同,结果仅包含单个子类类型的对象. 所有查询(包括查询和属性访问)在Realm中都是惰性的. 仅在访问属性时才读取数据.
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.
这篇关于LazyFilterBidirectionalCollection< Results< datatype>>'到预期的参数类型"[数据类型]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!