Filtering with the for-case statement

In this next example, we will use the for-case statement to filter through an array of tuples and print out only the results that match our criteria. The for-case example is very similar to using the where statement where it is designed to eliminate the need for an if statement within a loop to filter the results. In this example, we will use the for-case statement to filter through a list of World Series winners and print out the year(s) a particular team won the World Series:

var worldSeriesWinners = [  
  ("Red Sox", 2004), 
  ("White Sox", 2005), 
  ("Cardinals", 2006), 
  ("Red Sox", 2007), 
  ("Phillies", 2008), 
  ("Yankees", 2009), 
  ("Giants", 2010), 
  ("Cardinals", 2011), 
  ("Giants", 2012), 
  ("Red Sox", 2013), 
  ("Giants", 2014), 
  ("Royals", 2015) 
] 
 
for case let ("Red Sox", year) in worldSeriesWinners {  
  print(year) 
} 

In this example, we created an array of tuples named worldSeriesWinners, where each tuple in the array contained the name of the team and the year that they won the World Series. We then use the for-case statement to filter through the array and only print out the years that the Red Sox won the World Series. The filtering is done within the case statement, where ("Red Sox", year) states that we want all the results that have the string Red Sox in the first item of the tuple, and the value of the second item in the year constant. The for-in loop then loops through the results of the case statement, printing out the value of the year constant.

The for-case-in statement also makes it very easy to filter out the nil values in an array of optionals. Let's look at an example of this:

let myNumbers: [Int?] = [1, 2, nil, 4, 5, nil, 6] 
 
for case let .some(num) in myNumbers { 
  print(num) 
} 

In this example, we created an array of optionals named myNumbers that could contain either an integer value or nil. As we will see in Chapter 10, Using Optional Types, an optional is internally defined as an enumeration, as shown in the following code:

enum Optional<Wrapped> {  
  case none, 
  case some(Wrapped) 
} 

If an optional is set to nil, it will have a value of none, but if it is not nil it will have a value of some, with an associated type of the actual value. In our example, when we filter for .some(num), we are looking for any optional that has a non-nil value. As shorthand for .some(), we could use the ? (question mark) symbol, as we will see in the following example. This example also combines the for-case-in statement with a where statement to perform additional filtering.

let myNumbers: [Int?] = [1, 2, nil, 4, 5, nil, 6] 
 
for case let num? in myNumbers where num > 3 {  
  print(num) 
} 

This example is the same as the previous example, except that we have inputted the additional filtering with the where statement. In the previous example, we looped through all of the non-nil values, but in this example, we have looped through the non-nil values that are greater than 3. Let's see how we do this same filtering without the case or where statements:

let myNumbers: [Int?] = [1, 2, nil, 4, 5, nil, 6] 
 
for num in myNumbers {  
  if let num = num { 
    if num > 3 { 
      print(num) 
    } 
  } 
} 

Using the for-case-in and where statements can greatly reduce the number of lines needed. It also makes our code much easier to read because all the filtering statements are on the same line.

Let's look at one more filtering example. This time, we will look at the if-case statement.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset