O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the if-case statement

Using the if-case statement is very similar to using the switch statement. Most of the time the switch statement is preferred when we have over two cases we are trying to match, but there are instances where the if-case statement is needed. One of those times is when we are only looking for one or two possible matches, and we do not want to handle all the possible matches. Let's look at an example of this:

enum Identifier {  
  case Name(String)  
  case Number(Int)  
  case NoIdentifier 
} 
var playerIdentifier = Identifier.Number(2)  
if case let .Number(num) = playerIdentifier { 
  print("Player's number is \(num)") 
} 

In this example, we created an enumeration named Identifier that contains three possible values: Name, Number ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required