ios - Swift PrepareForSegue NSIndexPath error -
i want prepare segue via:
override func prepareforsegue(segue: uistoryboardsegue?, sender: anyobject?) { if segue?.identifier != "fromopenchatstologin" { if let controller: chatviewcontroller? = segue?.destinationviewcontroller as? chatviewcontroller { if let cell: onlineusercell? = sender as? onlineusercell { let user = oneroster.userfromrosteratindexpath(indexpath: tableview.indexpathforcell(cell!)!) controller!.recipient = user } } } }
where onlineusercell
custom cell. also, that's userfromrosteratindexpath:
class func userfromrosteratindexpath(indexpath indexpath: nsindexpath) -> xmppusercoredatastorageobject { return sharedinstance.fetchedresultscontroller()!.objectatindexpath(indexpath) as! xmppusercoredatastorageobject }
so, when select cell crashes with:
fatal error: unexpectedly found nil while unwrapping optional value
on line:
let user = oneroster.userfromrosteratindexpath(indexpath: tableview.indexpathforcell(cell!)!)
what wrong? how can fix it?
first of all, swift introduced type inference, , best practices use properly, write code this
if let controller = segue?.destinationviewcontroller as? chatviewcontroller { if let cell = sender as? onlineusercell { } }
also should set exception breakpoint in xcode (here), see more you're crashing.
in case in
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?)
you should use following statement :
if segue.identifier == "fromopenchatstologin" { } else { }
your login viewcontroller doesn't require user, therefore crashed at
let user = oneroster.userfromrosteratindexpath(indexpath: tableview.indexpathforcell(cell)!)
because there no cell
, , sender
in prepareforsegue
not cell.
for second issue, fix easy.
you sending oneroster.userfromrosteratindexpath(indexpath: indexpath)
in
performseguewithidentifier("fromuserslisttochatview", sender: oneroster.userfromrosteratindexpath(indexpath: indexpath))
so don't need cell or since have user
object !
set recipient
:
controller?.recipient = sender as? xmppusercoredatastorageobject
just replace prepareforsegue
method following , work:
if segue.identifier == "fromopenchatstologin" { } else { let controller = segue.destinationviewcontroller as? chatviewcontroller controller?.recipient = sender as? xmppusercoredatastorageobject }
Comments
Post a Comment