Pick out just the .Some cases using the ? operator:
let opts : [Int?] = [1, nil, 2, nil, nil, 5, nil, 3, 8, 11, 2, 6, nil, 9] // "1 2 5 3 8 11 2 6 9 " for case let x? in opts { print("\(x) ", appendNewline: false) } print("")
Pick out just the .Some cases using the ? operator:
let opts : [Int?] = [1, nil, 2, nil, nil, 5, nil, 3, 8, 11, 2, 6, nil, 9] // "1 2 5 3 8 11 2 6 9 " for case let x? in opts { print("\(x) ", appendNewline: false) } print("")
3 Comments
Wow! Where did you find this ‘case let’ inside a ‘for …in’?
I could not find anything like this in Swift docs…
It’s pretty much directly from the book in the Optional Pattern section. 🙂
That’s right, but it was not obvious that the “value binding pattern” would work in a “for … in”! Great trick!