chrismc chrismc - 1 year ago 94
Swift Question

Pattern matching in a Swift for loop

It seems like there should be a "Swiftier" way to do this - but I'm still wrapping my head around the various ways to do pattern matching in Swift.

Suppose I have an Array of AnyObject and I'd like to loop through it and if the item is an Int AND the Int is a multiple of 5, then I'd like to print it out. Here's my initial approach that works:

let myStuff: [AnyObject] = [5, "dog", 11, 15, "cat"]

for item in myStuff {
if let myInt = item as? Int where myInt % 5 == 0 {
print ("\(item)")

Honestly, that's not bad...but with all of Swift's pattern matching syntax, it seems like I should be able to consolidate the logic to 1 line. So far, I haven't found an approach that works - but I was expecting to be able to do something like:

//This doesn't work, but I feel like something similar to this should
for item in myStuff where item is Int, item % 5 == 0 {
print ("\(item)")

Obviously, this isn't a huge deal - but it's more a thought exercise for me to understand Swift's pattern matching a little better.

Answer Source

You can combine a pattern matching conditional cast with a where clause like so:

let myStuff: [AnyObject] = [5, "dog", 11, 15, "cat"]

// item will be an Int, and divisible by 5
for case let item as Int in myStuff where item % 5 == 0 {

// Prints:
// 5
// 15
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download