One of the things I love about Swift is that it really tries to get some of the boilerplate out if the way. This doesn't seem true when you write your first optional unwrapping, of course. Something like optional unwrapping doesn't seem like a big deal at first, but once you've written your 100th variation on:

if let unwrapped = optionalVariable as? String {
    //do something
}

Things seem to be a lot more wordy. I mean, you get it, Swift sort enforces safety, but dealing with those safety checks can start to feel tedious.

Enter Swift's desire to get ride of the uninteresting parts of your code. Take the humble for-in loop that we all know and love, for example.

for myString in stringArray {
    //do something
}

Seems reasonable enough, but let's assume that, instead of and array of String, we're dealing with an Array of AnyObject? and we want to print "found it" anytime we find an occurrence of 🍉 in our array.

"Not a problem," you think to yourself. First we have to iterate over the array, unwrap the optional while casting to a string, then checking to see if the string matches 🍉.

We'll probably end up with something like this:

for thing in watermelonPatch {
    if let possibleMelon = thing as? String {
        if possibleMelon == 🍉 {
            print("found it!)
        }
    }
}

Now, there isn't anything wrong with this code per se, but taking up a lot of visual space setting up for the one line where we actually do something, which is print("found it!). Everything else is pretty much just checking to see if all of the conditions are right to execute this line.

Let's refactor this loop to put emphasis on the part that's the most important. The first thing we'll do is move our condition check up into our optional unwrapping line.

for thing in watermelonPatch {
    if let possibleMelon = thing as? String where possibleMelon==🍉 {
        print("found it!)
    }
}

Nice. Looking better already. Swift's whereis incredible useful. You can use it in unwrapping statements like the above code or you can use it to add conditional checking to switch statements.

Speaking of switches, we can use the pattern matching you often see in switch statements to do our optional unwrapping in the for loop itself. Behold:

for case let possibleMelon as String in watermelonPatch where possibleMelon == 🍉 {
    print("found a watermelon!")
}

We've gone from 7 lines down to three and removed all of the nested bracketed statements. Now, the important work of announcing that the watermelon has been found isn't being lost in all of the conditional code around it.

Now, like everything, this could be carried too far. If we are inadvertently obscuring important code in our boilerplate reduction, we aren't gaining readability, which is the goal here. In the end, we are trying to help people who are reading the code to focus on the most important parts, rather than getting lost in setup code and boiler plate.