I'm new to swift and now learning from this book that is called The Swift Programming Language. In the book there is an example:
enum Rank: Int {
case Ace = 1
case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
case Jack, Queen, King
func simpleDescription() -> String {
switch self {
case .Ace:
return "ace"
case .Jack:
return "jack"
case .Queen:
return "queen"
case .King:
return "king"
default:
return String(self.rawValue)
}
}
}
And here is the part I don't understand:
if let convertRank = Rank(rawValue: 3){
let description = convertRank.simpleDescription()
}
I tried change the code above to this:
let convertRank = Rank(rawValue: 3)
let description = convertRank.simpleDescription()
Basically, I have just removed the if
statement, but then an error occurs: Value of optional 'Rank?' not unwrapped; did you mean to use '!' or '?'?
Why do I have to use an if
statement? And I don't really understand what does the error message say.