简体   繁体   中英

Get data from Parse.com as Variable

I'm pretty new to programming for the iPhone and have been following several tutorials and also using this site often (I must show huge gratitude here for the amount of support that has been given to me, I really hope that I can contribute and also help others).

I currently have a swift application that inserts data to Parse.com and also retrives it and displays it in the NSLog.

This is the code in the AppDelegate.swift file:

Parse.setApplicationId("xxxx", clientKey: "xxxx")   

    var gameScore = PFObject(className: "GameScore")
    gameScore.setObject(1337, forKey: "score")
    gameScore.setObject("Sean Plott", forKey: "playerName")
    gameScore.saveInBackgroundWithBlock {
        (success: Bool!, error: NSError!) -> Void in
        if (success ?? false) {
            NSLog("Object created with id: \(gameScore.objectId)")

        } else {
            NSLog("%@", error)
        }
    }

    var query = PFQuery(className: "GameScore")
    query.getObjectInBackgroundWithId("phNzKk6364") {

        (scoreAgain: PFObject!, error: NSError!) -> Void in

        if error == nil {
            NSLog("No Error: %@", scoreAgain.objectForKey("playerName") as NSString)

        } else {
            NSLog("%@", error)
        }
    }

As you can see in these lines of code, the value of that specific user has been taken and placed in the NSLog. How do I assign it to a variable instead and pass it to a UILabel.

I have created an instance of the ViewController like so:

let vcObj : ViewController = ViewController() 

and have tried adding this under the NSLog that outputs it.

self.vcObj.playerName.text = scoreAgain.objectForKey("playerName")

But I get this error

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

Can someone help me get the variable from Parse.com, Assign it to a variable and then pass it to the ViewController Label?

Thanks

The problem is that the Storyboard is creating a ViewController for you and then you are allocating a second one. Yours is not the one that is on screen, and since yours wasn't loaded from a Storyboard, your IBOutlet is nil .

Leave the Parse.setApplicationId("xxxx", clientKey: "xxxx") in AppDelegate.swift , but move the rest of your code to viewDidLoad in ViewController.swift . Then you'll be able to set your label because your IBOutlet won't be nil .

In your ViewController.swift file:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var playerName: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        println("viewDidLoad")
        // Do any additional setup after loading the view, typically from a nib.

        var gameScore = PFObject(className: "GameScore")
        gameScore.setObject(1337, forKey: "score")
        gameScore.setObject("Sean Plott", forKey: "playerName")
        gameScore.saveInBackgroundWithBlock {
            (success: Bool!, error: NSError!) -> Void in
            if (success ?? false) {
                println("Object created with id: \(gameScore.objectId)")
            } else {
                println(error)
            }
        }

        var query = PFQuery(className: "GameScore")

        query.getObjectInBackgroundWithId("phNzKk6364") {

            (scoreAgain: PFObject!, error: NSError!) -> Void in

            if error == nil {
                println("No Error: " + (scoreAgain.objectForKey("playerName") as String))
                self.playerName.text = scoreAgain.objectForKey("playerName") as String
            } else {
                println(error)
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM