试图访问一个SWIFT分析查询数组的下标 [英] trying to access the subscript of a Parse query array in SWIFT
问题描述
我有困难访问标在对象的数组已经从分析数据库中查询点零。 code:
I am having difficulty accessing the subscript at point zero of an array of objects that has been queried from Parses database. Code:
var query = PFUser.query()
query.whereKey("location", nearGeoPoint: geopoint, withinMiles: self.distance)
query.whereKey("objectId", notContainedIn: self.matches)
query.whereKey("objectId", notEqualTo: self.user.objectId)
query.limit = 500
var objects = query.findObjects()
if objects != nil {
for object in objects {
var closestUsers = object as PFUser
var closestUser = closestUsers[0] //COMPILER ERROR: Type 'String!' does not conform to protocol 'IntegerLiteralConvertible'
}
如图中code我得到一个编译器错误,指出:'串'类型不符合协议IntegerLiteralConvertible'`我不能工作了,为什么它不工作?
As shown in the code I am getting a compiler error stating: Type 'String!' does not conform to protocol 'IntegerLiteralConvertible'` I can't work out why It is not working?
先谢谢了。
推荐答案
我觉得很难理解你code的意思做,但在猜测...
I find it hard to understand what your code is meant to do, but at a guess...
尝试:
var query = PFUser.query()
query.whereKey("location", nearGeoPoint: geopoint, withinMiles: self.distance)
query.whereKey("objectId", notContainedIn: self.matches)
query.whereKey("objectId", notEqualTo: self.user.objectId)
query.limit = 500
var users = query.findObjects() as [PFUser]
var closestUser = users.firstObject
在您的code
var objects = query.findObjects() // Get response
if objects != nil { // Check if it is nil
for object in objects { // Iterate over the objects
var closestUsers = object as PFUser // Get the current user object (I presume here you want an array of users?)
var closestUser = closestUsers[0] // Get the first user?
}
的 VAR closestUsers =对象PFUser
并没有得到用户的阵列,而是当前的用户对象。
The var closestUsers = object as PFUser
doesn't get the array of users but instead the current user object.
我presume你想从数组的第一个用户对象。在这种情况下,你可以抓住从原数组的第一个对象,而无需遍历它。
I presume you want the first user object from the array. In which case you can just grab the first object from the original array without iterating over it.
VAR用户=(query.findObjects()作为?[PFUser])?firstObject
这篇关于试图访问一个SWIFT分析查询数组的下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!