Cannot call value of non-function type [PFObject] when retriving info from parse.com

90 Views Asked by At

I'm new to Swift and I'm learning how to use parse.com to store data an retrive it. I'm downloading an array of PFObjects from PARSE and then I need to turn it into a structure, so I created this function:

func queryDownload (user : PFUser) {

    let objects: [PFObject]
    let query = PFQuery(className: "Gluc")
    query.whereKey("user", equalTo: user)
    do {
        objects = try query.findObjects() as [PFObject]

    } catch {
        print("ERROR")

    }
    let returnedObjects = objects {
        let elements = self.returnedObjects.count
        for i in 0...elements-1 {
            self.dataArray.append(MyData(gluc: Int(self.returnedObjects[i]["meassure"] as! String)!, fec: self.returnedObjects[i]["fec"] as! Date, alimento: self.returnedObjects[i]["alim"] as! Int, comentarios: self.returnedObjects[i]["notes"] as! String))

        }
    }

    self.dataArrayOrdered = self.dataArray.sorted(by: { $0.fec.compare($1.fec) == .orderedAscending})

    print(self.dataArrayOrdered)
}

But I can't run it because in this line:

let returnedObjects = objects {

It sais "Cannot call value of non-function type [PFObject]"

I'm not sure how to avoid this problem, so any help would be appreciated

Thanks

1

There are 1 best solutions below

0
vadian On BEST ANSWER
let returnedObjects = objects { ... }

is a syntax error, you probably wanted to optional bind the value

if let returnedObjects = objects { ... }

but objects is non-optional and optional binding is not possible.


Just assign the value to the variable and remove the braces

do {
    let objects = try query.findObjects() as [PFObject]
    let returnedObjects = objects
    let elements = self.returnedObjects.count
    for i in 0...elements-1 {
        self.dataArray.append(MyData(gluc: Int(self.returnedObjects[i]["meassure"] as! String)!, fec: self.returnedObjects[i]["fec"] as! Date, alimento: self.returnedObjects[i]["alim"] as! Int, comentarios: self.returnedObjects[i]["notes"] as! String))
    }
    self.dataArrayOrdered = self.dataArray.sorted(by: { $0.fec.compare($1.fec) == .orderedAscending})
    print(self.dataArrayOrdered)
} catch {
    print("ERROR", error)
} 

It's very important to put all good code in the do block and print the actual error rather than the simple literal string "ERROR"

However this is Swift and there is a smarter and more convenient way using the map function

do {
    let objects = try query.findObjects() as [PFObject]
    self.dataArray = objects.map { MyData(gluc: Int($0["meassure"] as! String)!, fec: $0["fec"] as! Date, alimento: $0["alim"] as! Int, comentarios: $0["notes"] as! String) }
    self.dataArrayOrdered = self.dataArray.sorted(by: { $0.fec.compare($1.fec) == .orderedAscending})
    print(self.dataArrayOrdered)
} catch {
    print("ERROR", error)
}