1
votes

This code worked fine in Swift 1.1 ... just trying to figure out what's changed in 1.2 to make it incompatible:

@IBAction func load_click(sender: AnyObject) {

    var query = PFQuery(className: "myClass")
    query.getObjectInBackgroundWithId("MPSVivtvJR", block: { (object:PFObject!, error: NSError) -> Void in

        let theName = object["name"] as String
        let theAge = object["age"] as Int?

        println(theName)
        println(theAge)

    })
}

It gives me the error: Cannot invoke 'GetObjectInBackgroundWithId' with an argument list of type '(String, block: (PFObject!, NSError) -> Void)

Any ideas? Thanks!

3
According to the docs, object should be an NSArray, not a PFObject!. - matt
Thanks, matt--the same error persists, though--even making it an NSArray. - hudsonian
The NSArray thing is very odd, isn't it? Try structuring your code more like this example from the Parse blog (at end of page): blog.parse.com/2014/06/06/building-apps-with-parse-and-swift Maybe the problem is that you need NSError! with an exclamation mark. - matt
parse SDK v1.7.1 should fix this problem - nick9999
Still the same error in my code with SDK v1.7.1 - Fabian Boulegue

3 Answers

6
votes

Now with Swift 1.2 you are supposed to be more careful with unwrapping optionals. So inside the closure where you have PFObject and NSError, either remove the exclamation marks or add a question mark to make it optional.

Then, unwrap your object more safely. Try as follows:

// You can create this in a separate file where you save your models

struct myUser {
    let name: String?
    let age: Int?
}

// Now this in the view controller

@IBAction func load_click(sender: AnyObject) {
    var query = PFQuery(className: "myClass")
    query.getObjectInBackgroundWithId("MPSVivtvJR", block: {
        (object:PFObject!, error: NSError?) -> Void in

        if let thisName = object["name"] as? String{
            if let thisAge = object["age"] as? Int{
                let user = myUser(name: thisName, age: thisAge)
                println(user)
            }
        }

    })
}
2
votes

I struggled with this a lot, but the code below works for me.

    var query = PFQuery(className: "class")
    query.whereKey("user", equalTo: PFUser.currentUser()!)
    query.orderByDescending("createdAt")
    var object = query.getFirstObject()

    if let pfObject = object {
        data.variable = (pfObject["variable"] as? Float)!
    }
1
votes

The accepted answer doesn't work for me. Here's what did work:

var query = PFQuery(className: "score")
query.getObjectInBackgroundWithId("j5xBfJ9YXu", block: {
    (obj, error)in
    if let score = obj! as? PFObject {
        println(score.objectForKey("name"))
    } else {
        println(error)
    }
})