Tino
(Tino)
April 20, 2018, 10:47am
5
This pops up from time to time:
To me it would seem more logical as "for x in array? { }" — to parallel
"for case let x? in array { }"
··· On Fri, Feb 10, 2017 at 1:03 PM, Rick Mann via swift-users < swift-users@swift.org> wrote:
I love the idea of for in? (Or even for? In). You should pitch that to
evolution.
Sent from my iPhone
On Feb 10, 2017, at 07:04, Tino Heth <2th@gmx.de> wrote:
Is there any concise way to write the following?
if let collection = someOptionalCollection
{
for item in collection
…
This one started over at swift-users, with a question on how to deal with looping over containers that may be nil.
Imho the beauty of the feature is that it's simple enough to be explained in the subject line, but here is the "long" story:
let test: [Int]? = nil
// this is possible now
if let test = test {
for i in test {
print(i)
}
}
// how it could be written with a modified keyword
for i in? test {
print(i)
}
I've been thinking "in?" had been brought up long ago, but as…
... and there are more threads with possible "workarounds".
Is what I do most of the time as well -- although I alway feel bad because I never checked if this actually creates an empty array with the associated overhead compared to if let
(but in most cases, overhead doesn't matter anyways...)