guard let foo = foo VS guard foo != nil in Swift

With your first example, foo becomes a non-Optional. So, you can do this:

guard let foo = foo else { return }
foo.doMethod()

Whereas without the optional binding, you'd still have an Optional:

guard foo != nil else { return }
foo?.doMethod()

There are plenty of times when having a non-Optional value is easier to deal with, including being able to avoid force unwrapping with !, so there's lots of practicality to the first code sample.

In terms of the second version, you could use it where you want to do a nil check but may not actually use the value in the scope in which you're checking it.


In your example with the array of type [String], yes, you can do your second comparison without the optional binding to check to see if there's a last element:

guard array.last == "corn" else { return false }

You are correct that it is cleaner this way. It is extremely unlikely that it would be more "memory efficient" as you speculated, though, as the compiler would likely optimize away the temporary optional binding.