0
votes

The code below is giving me the error " 'AnyObject' is not convertible to 'String' " at the line where I put my "if let" statement to unwrap the optional productData pulled from Parse. I'm just trying to pull a String from an object in Parse. I've checked everywhere and all the standard answers/solutions aren't working. Any thoughts?

Most of this code is taken straight from the Parse iOS docs Here

import Foundation

import Parse

func getDataFromParse () {

var productDataFromParse = PFQuery(className:"Product")

productDataFromParse.getObjectInBackgroundWithId("uyeXHufDgq") {

(productData: PFObject?, error: NSError?) -> Void in

if error == nil && productData != nil {

    if let productTitle = productData["productTitle"] as! String {
        self.productTitle = productTitle
    }


} else {
    println(error)
}
}

}
1
Was your problem solved or do you have more questions? If it has, please select one of the answers. If not, let me know and I'll try to help. - Tim Bodeit

1 Answers

0
votes

productData: PFObject? object is an optional itself. You can't subscript over it yet, because it needs to be unwrapped before. You can replace the productData != nil check with optional chaining.

In Swift 1.2 you can do that and your error checking in the same statement:

if let productTitle = productData?["productTitle"] as? String
   where error == nil {
    self.productTitle = productTitle
}

Note the additional ? between the productData and the square brackets.