简体   繁体   中英

Get ObjectIds from parse.com in swift

I am making a small quiz game and I want to get objectIds from parse.com automatically. Till now I have been adding all the ObjectIds to an string array. How do I get object ids from online automatically.

My code till now is:

func GetRandomObjectId(){
    ObjectIds = ["AJ1jRqzmrk", "9OUWzaVPfg", "eyVmX1nX9T", "KeiJNXrCk5", "4f6SJToxFB", "kYvrpQ5jjD", "WkZAYw69RO", "foKgndANk8", "Wlk5oKBtho", "KjY7cNrP7R", "melGedvF97", "3pmHO8It1V", "0b8yyJ5xs3", "Ks3IpswvgB", "cePOow18sl", "f44AnNuq7u", "iOC3DJIgl3", "nZNjzjtJXY", "K5osjVrXUh", "IJxk2DpLAu", "7o1IO7L3TN", "bNlpRPwU69", "RzHDdn4St9", "R0TNSiQfHp", "p0pI7BkcPn", "tynypQu322", "joJOnvg1GA", "ouc0cIkZAP", "LQyx2wddUD", "qGmTCEQtqt", "axqhlftacj", "0J9vPeiJLb", "bHuaJFKr2a", "BUcFyOIe0N", "7SphNF9PfT", "401hhCwaJh", "VBfT2DlG9o", "w5H5k5s4PO", "qGuQRUBaVZ", "GHPXUgunix", "CkjpEkc4j9", "tAGHm9s11V", "fnejJDRdO2", "rvlPPWj8lg", "gXLpecJer7", "Opqz3pWQGE", "30bnxuNsKg", "eGHIMovYfh", "N8V4YU85XR", "WlneMojPL4", "k8sxngp4G5", "QeiMTHiU57", "X7SXm5KNJM", "U4U42gJKl5", "RaMyohu0by", "4CE0Y3aki3", "y56V1ZCuHd", "u6U9VEM3cI", "KhsKiOIqrs", "eGmcuK1CJ3", "CXn1x3Qzkp", "voTrf4BckP", "bY0oQ2XXDy", "eYHY55HHyO", "n3qpO3nclq", "yscxjyXPkU", "1wAGzd3Yl7", "F8nWM556wS", "wBJEzGrNxd", "BiCAIhd1WT", "fWKxJ7l2Sd", "xCeVXha724", "kdjBJQYnTV", "5BsWbdsneU", "np0m6jVY0s", "R9Np83nVUj", "KmCMM1nXEe", "ycuinn8gpw", "UlzTPub6Ko", "hIkiA20u6e", "t63WHfOJHd", "4ppWsrza3E", "HHboXhXROk", "GJfCgrg8rm", "j5IT3hw7m4", "5Vl1KTve2A", "3kwKOPmw6S", "T1HH9BEBhx", "MAIXDco2WV", "OdVsHBOaUQ", "bdWoRGlyWU", "4ede2oXQ7c", "6PtgZJuzUA", "BSIKFs3HdC", "qUoWeXxmjb", "npIqlRmO90", "GY3RAgOKVl", "JtYkkJXMqM", "RmQNYFRSFn", "SGhsWdnlcP"]
    RandomId = Int(arc4random_uniform(UInt32(ObjectIds.count)))


}

func CallData(){

    GetRandomObjectId()

    var query = PFQuery(className: "Questions")
    query.getObjectInBackgroundWithId(ObjectIds[RandomId]){

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

        if(error == nil){

            self.Question = ObjectHolder["Question"] as String!
            self.Answers = ObjectHolder["Answers"] as Array!
            self.Answer = ObjectHolder["Answer"] as String!

            if(self.Answers.count > 0){

                self.QuestionLabel.text = self.Question
                self.QuestionLabel.font = UIFont(name: "HelveticaNeue-Thin", size: 30)
                self.Button1.setTitle(self.Answers[0], forState: UIControlState.Normal)
                self.Button1.titleLabel?.font = UIFont(name: "HelveticaNeue-Thin", size: 25)
                self.Button1.setTitleColor(self.UIColorFromRGB(0x096593), forState: UIControlState.Normal)
                self.Button2.setTitle(self.Answers[1], forState: UIControlState.Normal)
                self.Button2.titleLabel?.font = UIFont(name: "HelveticaNeue-Thin", size: 25)
                self.Button2.setTitleColor(self.UIColorFromRGB(0x096593), forState: UIControlState.Normal)
                self.Button3.setTitle(self.Answers[2], forState: UIControlState.Normal)
                self.Button3.titleLabel?.font = UIFont(name: "HelveticaNeue-Thin", size: 25)
                self.Button3.setTitleColor(self.UIColorFromRGB(0x096593), forState: UIControlState.Normal)
                self.Button4.setTitle(self.Answers[3], forState: UIControlState.Normal)
                self.Button4.titleLabel?.font = UIFont(name: "HelveticaNeue-Thin", size: 25)
                self.Button4.setTitleColor(self.UIColorFromRGB(0x096593), forState: UIControlState.Normal)

            }


        }else{
            NSLog("Error")
        }


    }

}

Try this.

let query = PFQuery()
let objectIDArray = [String]() // Array for object ids
query.findObjectsInBackgroundWithBlock{(objects,error)-> Void in{
if error == nil {
for object in objects{
    var objectID = object["objectId"] as String
    objectIDArray.append(objectID)
}

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