[swift-users] for with optional collection?

Saagar Jha saagar at saagarjha.com
Thu Feb 9 15:31:25 CST 2017


Or even

for item in someOptionalCollection ?? [] {
	item.doSomething()
}

Saagar Jha

> On Feb 9, 2017, at 1:30 PM, Jeff Kelley via swift-users <swift-users at swift.org> wrote:
> 
> You can do something like this:
> 
> someOptionalCollection?.forEach { item in
> 	item.doSomething()
> }
> 
> Or this:
> 
> (someOptionalCollection as? [SomeType])?.forEach { item in
> 	item.doSomething()
> }
> 
> Jeff Kelley
> 
> SlaunchaMan at gmail.com <mailto:SlaunchaMan at gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan> | jeffkelley.org <http://jeffkelley.org/>
>> On Feb 9, 2017, at 4:26 PM, Rick Mann via swift-users <swift-users at swift.org <mailto: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 <mailto: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/20170209/5334011f/attachment.html>


More information about the swift-users mailing list