1

Working with PFObject and PFQuery I am having trouble debugging this piece of code:

.......

if let someContents = object.valueForKey("contents") {
    let query = PFQuery(className: "TheContentList")
    do {let object = try query.getObjectWithId(someContents.objectId)
        print(object)
    } catch {
        print(error)
    }
}

With the code above I get this compiler message for the line with getObjectWithId:

Cannot convert value of type 'String?!' to type 'String' in coercion

If I change:

query.getObjectWithId(someContents.objectId)

to:

query.getObjectWithId("xyz23AcSXh")

It compiles and inside the debugger I get this:

(lldb) p someContents.objectId
(String?!) $R4 = "xyz23AcSXh"

And the program prints an object as expected.

So the question is: how should I write the line query.getObjectWithId to be able to use what is inside someContents?

Michel
  • 10,303
  • 17
  • 82
  • 179
  • Did you try with `query.getObjectWithId(someContents.objectId!)` .. check the exclamation mark. – José Roberto Abreu Jul 19 '16 at 02:16
  • What you write does not work. I get the error message from the compiler: `Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?` But following on this: `query.getObjectWithId(someContents.objectId!!)` works. So you put me on the right track. Thanks a lot. – Michel Jul 19 '16 at 02:31

1 Answers1

1

Your property objectId, is an Explicitly Unwrapped Optional, of an Optional. If you're sure it contains a String, unwrap it using:

let object = try query.getObjectWithId(someContents.objectId!!)

Otherwise, if you're not sure:

if let objectId = someContents.objectId, id = objectId {
    let object = try query.getObjectWithId(id)
}
paulvs
  • 11,963
  • 3
  • 41
  • 66