samedi 25 avril 2015

Cant't take an object id from a parse object and use it in other sections of code


Ive spent countless hours trying to figure out why I can't seem to take the object id of an object that has been saved to parse and use it in other sections of my code. Yes I have searched and found topics about this on stack overflow and other websites and I have read the parse.com documentation but none of the answers will allow me to fix this issue. I am pretty new to coding and I am only familiar with swift at the moment. If you can help me figure this out I would more than appreciate your help.

I declare objectID as a string variable at the top of the code. Later when a send button is tapped I save the data to parse successfully and attempt to capture the datas objectId by passing the objectID variable to the saveToParse function as an inout. The project does not show any errors. The code builds and runs. NSLog(id) shows me the proper objectId that I want in the console. However it does not show anything when NSLog(self.objectID) is called outside of the function.

Like I said, I'm trying to use this objectId outside the function in other parts of the code, but it just doesn't want to save the objectId to my variable. I've tried many other methods and this is the closest I have got to making it work.

Im new at this so hope I've explained my situation clearly, if you need any more information to solve this don't hesitate to ask. Thanks for reading :)

var objectID:String = String() // declared at the top (underneath Class)

@IBAction func sendQuestionTapped(sender: UIButton) {

    // Send question and answers to Parse
        saveToParse(&objectID)



    // Save Question object ID

        NSLog(self.objectID)  // Test if objectID is still Question.objectId

    // Move to the results view controller with information
    performSegueWithIdentifier("toResultsSegue", sender: self)


}

func saveToParse(inout id:String) {

    var Question = PFObject(className:"Question")
    Question["question"] = questionToPass
    Question["answers"] = arrayToPass

    Question.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            // The object has been saved.
            dispatch_async(dispatch_get_main_queue()) {

                id = Question.objectId!
                NSLog(id)

            }

        }

        else {

            // There was a problem, check error.description
            if error != nil {

                NSLog(error!.localizedDescription)
            }
        }
    }
}


Aucun commentaire:

Enregistrer un commentaire