You can do something like this:

someOptionalCollection?.forEach { item in
        item.doSomething()
}

Or this:

(someOptionalCollection as? [SomeType])?.forEach { item in
        item.doSomething()
}

Jeff Kelley

[email protected] | @SlaunchaMan <https://twitter.com/SlaunchaMan> | 
jeffkelley.org <http://jeffkelley.org/>
> On Feb 9, 2017, at 4:26 PM, Rick Mann via swift-users <[email protected]> 
> 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
> [email protected]
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to