0

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
        }
    }
}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Trey R
  • 21
  • 1
  • 5

1 Answers1

1

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.

Yichen Zhou
  • 436
  • 6
  • 21
  • Hi, I tried to use the official Syntax Convert Tool and it says there aren't any source changes necessary and I am still let with an error. Any thoughts? – Trey R Apr 19 '15 at 21:04