dimanche 19 mars 2017

Swift 3 unwrap and compare int in if

I'm trying to unwrap a value and, if it works, compare it to be greater than 0.

I'm doing the following way:

if let movieLength = movie.length as? Int, movieLength > 0 {
    self.durationLabel.text = "\(movieLength) min"
} else {
    self.durationLabel.isHidden = true
}

But I get the error

Downcast from 'Int?' to 'Int' only unwraps optionals; did you mean to use '!'?

Also tried using the where keyword but no success:

if let movieLength = movie.length as? Int where movieLength > 0 {
    self.durationLabel.text = "\(movieLength) min"
} else {
    self.durationLabel.isHidden = true
}

with the error:

Expected ',' joining parts of a multi-clause condition

Which I just tried above.

Aucun commentaire:

Enregistrer un commentaire