[swift-users] for with optional collection?

Maxim Veksler maxim at vekslers.org
Sun Feb 12 08:34:44 CST 2017


How about:

let c: [Int]? = nil

c.map { for e in $0 { print(e) } }

// 1

// 2

// 3

Based on
https://developer.apple.com/reference/swift/optional/1539476-map#discussion

On Thu, Feb 9, 2017 at 11:26 PM Rick Mann via swift-users <
swift-users at swift.org> wrote:

> Is there any concise way to write the following?
>
> if let collection = someOptionalCollection
> {
>     for item in collection
>     {
>     }
> }
>
> I can imagine more complicated things, too:
>
> if let collection = someOptionalCollection as? [SomeType]
> {
>     for item in collection
>     {
>     }
> }
>
> It would be nice to be able to just attempt to iterate on an optional
> collection (or Sequence?) and not have to write the enclosing if block
>
> Thanks!
>
> --
> Rick Mann
> rmann at latencyzero.com
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170212/175d2da3/attachment.html>


More information about the swift-users mailing list