Parse and Swift 1.2 issue

hudsonian

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!

iagomr

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

    })
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Initialization issue in Swift 2

From Dev

Parse Issue / Expected a type for CGFloat in Swift

From Dev

Parse Issue / Expected a type for CGFloat in Swift

From Dev

Batch fetch Parse and Swift 2

From Dev

parse json object in swift 2

From Dev

How to parse JSON in Swift 2?

From Dev

Batch fetch Parse and Swift 2

From Dev

Issue with Swift 2 Error Handling

From Dev

Swift 2: Alamofire GET Issue

From Dev

Iterating Capturing Issue with Swift 2

From Dev

Swift 1.2 and Parse: Issue with retrieving images to populate PFQueryCollectionViewController

From Dev

Swift issue when using Parse's signup in background with block

From Dev

NSPredicate 'IN' operator formatting issue in swift. Unable to parse the format string

From Dev

Swift 1.2 and Parse: Issue with retrieving images to populate PFQueryCollectionViewController

From Dev

Swift 2 parse Json as Optional to array

From Dev

Parse.com subclassing in Swift 2

From Dev

Getting Twitter profile image with Parse in Swift 2

From Dev

Swift 2 Parse and KingFisher cache images

From Dev

Parse.com subclassing in Swift 2

From Dev

Parse push notification in Swift 2 iOS 9

From Dev

Sign Up Not Working - Parse - Swift 2

From Dev

querying an object with a pointer - parse, swift2

From Dev

Swift 2 - Protocol conforming to Equatable issue

From Dev

Getting number from String Swift 2 Issue

From Dev

Issue with Google Analytics in Swift 2 or 3

From Dev

Getting number from String Swift 2 Issue

From Dev

Parse issue when using Change command (Rebol2)

From Dev

Strange issue in converting from Swift 2 to Swift 3

From Dev

Parse '1/2' from string Using Javascript

Related Related

HotTag

Archive