3

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?

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

From the Parse documentation on PFObjects

objectId, createdAt, and updatedAt are provided as properties on every PFObject, so you don't subscript them using [""], but instead reach them through .objectId, .createdAt, .updatedAt on your object.

You also don't need to force downcast to a String, as objectId is always a String (as you mentioned). But, you do need to unwrap the optional (saying that you know this objectId exists). Therefore the correct line of code should be:

let ObjectIdFromParse = oneObj.objectId!

Comments

1

If you want to store a representative common of .objectID, you should use the object's "uriRepresentation" (URL) that refers to that .objectID.

From documentation

func managedObjectID(forURIRepresentation: URL) -> NSManagedObjectID?

As an example, you have: swift 4

do
{
  let entity = try self.context.fetch(fetchRequest())
  let entityObj = entity.last as! Any
  let uriID = entityObj.objectID.uriRepresentation().absoluteString
}
catch
{ 
  print("Error...") 
}

Remember to saveContext() before you get the ID, or you will be given a temporary ID.

https://developer.apple.com/documentation/coredata/nsmanagedobjectid/1391691-temporaryid

You can find more: https://developer.apple.com/documentation/coredata/nspersistentstorecoordinator/1468882-managedobjectid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.