ios - Swift 3: prepare(for:sender:) -
this question has answer here:
i have simple entity:
extension team { @nonobjc public class func fetchrequest() -> nsfetchrequest<team> { return nsfetchrequest<team>(entityname: "team") } @nsmanaged public var name: string? @nsmanaged public var phone: string? @nsmanaged public var department: string? @nsmanaged public var position: string? }
picture easy understanding:
i load current entity core data app successfully. furthermore, have uitableviewcontroller property members (it's storage of fetch result team entity).
var members: [team] = []
after app launching (viewdidload(_:)) members.count equal 7 , right. also, these elements of members using uitableview in uitableviewcontroller:
my task open detailed view controller retrieve data tapped cell. delegation use classic prepare(for:sender:) method in uitableviewcontroller (who segue.source in current case):
// mark: - navigation extension teamlistviewcontroller { override func prepare(for segue: uistoryboardsegue, sender: any?) { guard segue.identifier == segueidentifier, let memberdetailsviewcontroller = segue.destination as? memberdetailsviewcontroller else { fatalerror("wrong destination!") } guard let selectedindex = tableview.indexpathforselectedrow?.row else { fatalerror("out of range") } guard let name = members[selectedindex].name, let phone = members[selectedindex].phone, let department = members[selectedindex].department, let position = members[selectedindex].position else { fatalerror("no correct data") } memberdetailsviewcontroller.membernamelabel.text! = name memberdetailsviewcontroller.memberphonenumberlabel.text! = phone memberdetailsviewcontroller.memberdepartmentlabel.text! = department memberdetailsviewcontroller.memberpositionlabel.text! = position } }
so, app launches, tap cell whichever want open more details and.. app crashes!
fatal error: unexpectedly found nil while unwrapping optional value
however, variables (name, phone, department, position) ok. values!
oh, yes. try retrieve data uilabels in memberdetailsviewcontroller below:
// mark: - iboutlets @iboutlet weak var membernamelabel: uilabel! @iboutlet weak var memberphonenumberlabel: uilabel! @iboutlet weak var memberdepartmentlabel: uilabel! @iboutlet weak var memberpositionlabel: uilabel!
what can problem here?
because of set data in prepare(for segue: uistoryboardsegue, sender: any?)
method in teamlistviewcontroller
memberdetailsviewcontroller
not loaded thats way got nil
.
my suggestion is: add object var members: team?
in memberdetailsviewcontroller
, pass teamlistviewcontroller
error not appear.
wiki
Comments
Post a Comment