Swift struct is nil when referenced from a function -
i set struct
this:
struct person { var name: string var age: int init(name:string, age:int) { self.name = name self.age = age } }
if in viewdidload
, happens expected.
override func viewdidload() { super.viewdidload() let person = person(name: "sally", age: 20) self.titlelabel.text = person.name }
however, if put same code in function
, , call function elsewhere
func setup() { let person = person(name: "wally", age: 12) self.titlelabel.text = person.name }
the app crashes on line label set, message:
"fatal error: unexpectedly found nil while unwrapping optional value"
what missing here?
Comments
Post a Comment