typealias shorthand = (String, String) -> (Str)
Table of Contents
But why shorthand?
Me: Largely because closures don’t think for themselves, they do as they’re told.
Closures: That is not true!
Me: Yes it is!
Closures: Okay.
Closures are amazing. I love them. They are an essential ingredient that helps make Swift great! Here are some secrets about them I had a hard time finding myself.
Given this code:
let screwyAlphabet = "qiwcghxkberdsljpvumyaoznft"
// closure 1
var sortedAlphabet = screwyAlphabet.sorted { (a, b) -> Bool in
return a < b
}
// closure 2
sortedAlphabet = screwyAlphabet.sorted(by: { (a, b) -> Bool in
a < b
})
// closure 3
sortedAlphabet = screwyAlphabet.sorted { $0 < $1 }
Without much experience with closures, you might quickly see that both closure 1
and closure 2
would appear to get the same result, but closure 3
looks a bit odd. To help speed things up, I’ll tell you now that they all three have the exact same result.
closure 1
probably feels the safest and most familiar to you. That’s okay and it’s a valid way of writing them, but Swift’s compiler is smart. It knows that this closure needs a return value (Bool
), as that’s a very common feature of them. Because this is apparent to the compiler, you may omit the return portion of the return statement and just skip right to the good part, resulting in closure 2
.
However, closure 3
is a bit crazy! This is Swift’s closure shorthand syntax. It knows what the return type is and autopopulates the $n
0 indexed variables with the according value passed in from the closure’s call. So, in this case, $0
is the equivalent of our earlier a
variable and $1
is the equivalent of b
. We then compare the two and return a Bool
. And once you understand this, it makes the code clean and easy to read.
While this is NOT valid code, you can think of it being written like this:
sortedAlphabet = screwyAlphabet.sorted(by: { ($0, $1) -> Bool in
$0 < $1
})
… Just don’t actually do that and go right to the short hand version (closure 3
).
But why shorthand?
You serious? I just… I just told you that a moment ago.
But if you really want to, you can get a copy of the code at the repo as usual.
But why shorthand?
Me: Largely because closures don’t think for themselves, they do as they’re told.
Closures: That is not true!
Me: Yes it is!
Closures: Okay.
Closures are amazing. I love them. They are an essential ingredient that helps make Swift great! Here are some secrets about them I had a hard time finding myself.
Given this code:
let screwyAlphabet = "qiwcghxkberdsljpvumyaoznft"
// closure 1
var sortedAlphabet = screwyAlphabet.sorted { (a, b) -> Bool in
return a < b
}
// closure 2
sortedAlphabet = screwyAlphabet.sorted(by: { (a, b) -> Bool in
a < b
})
// closure 3
sortedAlphabet = screwyAlphabet.sorted { $0 < $1 }
Without much experience with closures, you might quickly see that both closure 1
and closure 2
would appear to get the same result, but closure 3
looks a bit odd. To help speed things up, I’ll tell you now that they all three have the exact same result.
closure 1
probably feels the safest and most familiar to you. That’s okay and it’s a valid way of writing them, but Swift’s compiler is smart. It knows that this closure needs a return value (Bool
), as that’s a very common feature of them. Because this is apparent to the compiler, you may omit the return portion of the return statement and just skip right to the good part, resulting in closure 2
.
However, closure 3
is a bit crazy! This is Swift’s closure shorthand syntax. It knows what the return type is and autopopulates the $n
0 indexed variables with the according value passed in from the closure’s call. So, in this case, $0
is the equivalent of our earlier a
variable and $1
is the equivalent of b
. We then compare the two and return a Bool
. And once you understand this, it makes the code clean and easy to read.
While this is NOT valid code, you can think of it being written like this:
sortedAlphabet = screwyAlphabet.sorted(by: { ($0, $1) -> Bool in
$0 < $1
})
… Just don’t actually do that and go right to the short hand version (closure 3
).
But why shorthand?
You serious? I just… I just told you that a moment ago.
But if you really want to, you can get a copy of the code at the repo as usual.