trying to access the subscript of a Parse query array in SWIFT

送分小仙女□ 提交于 2019-12-12 01:49:36

问题


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'
    }

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?

Thanks in advance.


回答1:


I find it hard to understand what your code is meant to do, but at a guess...

Try:

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

In your 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?
}

The var closestUsers = object as PFUser doesn't get the array of users but instead the current user object.

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 user = (query.findObjects() as? [PFUser])?.firstObject




回答2:


Your logic is weird and you either typed something wrong or I'm completly not getting what you're doing.

Either way, you have an array of objects that are of type PFUser.

You put that array in another array (var closestUsers). And then you apparently want a single user out of it.

have you tried simply doing it like so :

and not having to cast everything around like crazy

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 closestUser = objects.firstObject;
    }

You'd maybe have to add as PFUser somewhere but I don't know swift so I don't know where to.



来源:https://stackoverflow.com/questions/29416107/trying-to-access-the-subscript-of-a-parse-query-array-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!