Search code examples
iosswiftparse-platformxcode6.3pfobject

Error when trying to Segue a PFObject to the next View Controller (Swift 1.2)


I am a newb to coding. I had no issues with creating a segue prior to the Swift 1.2 update. I am getting the error of Cannot assign a value of type 'PFObject' to a value of type 'PFObject?' when I try the code below. Can anyone help me figure this out ?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "PushToDetails" {

    // Get the new view controller using [segue destinationViewController].
    var detailScene = segue.destinationViewController as! DetailViewController

    // Pass the selected object to the destination view controller.
    if let indexPath = self.tableView.indexPathForSelectedRow() {
        let row = Int(indexPath.row)

        detailScene.currentObject = objects[row] as! PFObject
        }
    }
}

Solution

  • Your error said can not assign a value of type PFObject to a value of type PFObject!. See the difference of between the PFObject and PFObject!?

    • Because PFObject! is the type of Optional. Just like String and String!.They are the different types.
    • Like what you written for detailScene.currentObject = objects[row] as! PFObject, the currentObject is the Optional type probably if you check it, which leads detailScene.currentObject to the Optional type.
    • After that you assign the objects[row], the PFObject type, to detailScene.currentObject, the PFObject! type.


    Maybe you can correct your code like this,

        detailScene.currentObject! = objects[row] as! PFObject
    

    Unwrapped the currentObject to the PFObject type.