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

Tim Vermeulen tvermeulen at me.com
Thu Aug 4 14:41:29 CDT 2016


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