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

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", ...

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