[swift-users] Cleaner way than if let initialization?

Daniel Tartaglia danielt1263 at gmail.com
Thu Aug 4 15:18:19 CDT 2016


Thanks Tim!

> On Aug 4, 2016, at 3:41 PM, Tim Vermeulen <tvermeulen at me.com> wrote:
> 
> You want `flatMap`:
> 
> let dobString = dob.flatMap(serverDateFormatter.stringFromDate)
> 
> Or if you want `dobString` to be non-optional:
> 
> let dobString = dob.flatMap(serverDateFormatter.stringFromDate) ?? “"
> 
>> Currently I do stuff like this:
>> 
>> letdobString:String
>> ifletdob = dob {
>> dobString =serverDateFormatter.stringFromDate(dob)
>> }
>> else{
>> dobString =""
>> }
>> 
>> Is there a better, more idiomatic, way to do this sort of thing?
>> 
>> 
>> 



More information about the swift-users mailing list