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.
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