实例成员 'query' 不能用于类型 'GeoFire';你的意思是要使用这种类型的值吗?

问题描述

我怎样才能获得这个 GeoFire 的价值?

错误消息:实例成员 'query' 不能用于类型 'GeoFire';您是想改用这种类型的值吗?

 let center = CLLocation(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)
                
                let circleQuery = GeoFire.query(at: center,withRadius: 20);
                
                circleQuery.observe(.keyEntered) { (key,location,snapshot) in
                              // parse the snapshot for your result.
                               print("Key '\(key)' entered the search area and is at location '\(location)'")
                           }

原始代码How to query nearest users in Firebase with Swift?

解决方法

从您链接的问题和 documentation 来看,该方法似乎被调用

geoFire.queryAtLocation(center,withRadius: 20);

另请注意,这两个位置都表明您需要初始化 GeoFire 的实例。你不能只在类上调用查询方法。

,

总结一下我上面的评论:

  • 您需要创建一个 GeoFire 实例来访问查询方法。
  • 要创建该实例,您需要先将其连接到 Firebase 数据库(firebaseRef 需要指向数据库中的任何位置)。

因此:

let geofireRef = Database.database().reference()
let geoFire = GeoFire(firebaseRef: geofireRef)

然后,要查询该实例,只需使用它:

let center = CLLocation(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)
                
let circleQuery = geoFire.query(at: center,withRadius: 20);

然后继续观察circleQuery