I have a class ScoreModel
simplified to:
class ScoreModel {
var teamName : String!
}
Which could be placed in an Init()
function since it needs to always be set, but that does not help my understanding of the problem:
I have a ScoreViewController : UIViewController
where I use the non-optional ScoreModel
:
class ScoreViewController {
var scoreModel : ScoreModel!
}
which is passed using prepareForSegue
. This is all working but when i want to get the teamName it is returned as optional:
override func viewWillAppear(_ animated : Bool) {
let name = scoreModel.teamName
print(name)
}
this outputs:
Optional("teamNameEntered")
But the variable is marked with an exclamation mark in the ScoreModel so it should be unwrapped. Why is it still returning an Optional?
EDIT: as requested the prepareForSegue method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? ScoreViewController {
let scoreModel = self.scoreModel ?? ScoreModel()
destinationVC.scoreModel = scoreModel
}
}