【问题标题】:Instance member 'query' cannot be used on type 'GeoFire'; did you mean to use a value of this type instead?实例成员 'query' 不能用于类型 'GeoFire';你的意思是使用这种类型的值吗?
【发布时间】:2021-02-02 13:40:20
【问题描述】:

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

错误消息:实例成员“查询”不能用于类型“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?

【问题讨论】:

  • 查看原码:let geofireRef = /* ... */; let geoFire = GeoFire(firebaseRef: geofireRef); geoFire.query(/*...*/)
  • 我做了这个,现在没有任何错误,但应用程序崩溃了......
  • 因为空数组...(线程 1: "*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray")
  • 我在你的代码中没有看到任何数组;您必须提供更多信息才能显示崩溃的确切位置。
  • var geofire = GeoFire(firebaseRef: querySnapshot!.documents[0].get("koordinaten") as!DatabaseReference)

标签: swift firebase geofire


【解决方案1】:

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

geoFire.queryAtLocation(center, withRadius: 20);

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

【讨论】:

  • queryAtLocation(_:withRadius:)' 已重命名为 'query(at:withRadius:)'
【解决方案2】:

总结一下我上面的cmets:

  • 您需要创建一个 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

【讨论】:

  • 输出为:“
  • 我不懂你们这些cmets。首先,what 输出是什么意思? print() 将输出类似Key ... 的内容。那么,第二个设备是什么?
  • 好的,我想查看我所在位置周围的所有人。我已经使用 didUpdate 位置实现了位置管理器并将其写入 firebase 数据库。现在我想在我的区域中获取用户的新功能....
  • 好的,但这是一个完全不同的问题。您应该接受这一点并创建一个新的,并提供更多代码/示例数据。
猜你喜欢
  • 2021-06-29
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多