Search code examples
iosswiftobjectparse-platform

How to grab ObjectId from object in parse and present it as string in swift


Why am I not able to present the "ObjectId" from an Object from parse as a String in swift even though it is naturally already a string? Here is the logic I a attempting to you use:

//MARK: Fetch ObjectId Strings From Parse
    func fetchObjectIdString() {

        let query = PFQuery(className: "Books")
        query.whereKey("genreName", equalTo: self.genreName)
        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

            if error == nil {

                if let objects = objects as [PFObject]! {

                    for oneObj in objects {

                        let ObjectIdFromParse = oneObj["objectId"] as! String
                        let SingleObjectId = SixthBook()
                        SingleObjectId.objectIdString = ObjectIdFromParse
                        self.arrayOfObjectId.append(SingleObjectId)
                    }
                }
            } else {

                // Log details of the failure
                print("Error: \(error!) \(error!.userInfo)")
            }
        }
    }

This works fine for all other string columns for my parse objects except the ObjectId. It throws an exception error everytime. Why is this?


Solution

  • Use oneObj.objectId instead of what you tried in the above code.

    Change the line let ObjectIdFromParse = oneObj["objectId"] as! String to

    let ObjectIdFromParse = oneObj.objectId as! String