> On Aug 4, 2016, at 11:32 AM, Daniel Tartaglia via swift-users
> <swift-users@swift.org> wrote:
>
> Currently I do stuff like this:
>
> let dobString: String
> if let dob = dob {
> dobString = serverDateFormatter.stringFromDate(dob)
> }
> else {
> dobString = ""
> }
>
> Is there a better, more idiomatic, way to do this sort of thing?
So if I understand this correctly `dob` is an optional date, and `dobString` is
a non-optional String. Right?
If that's what you're dealing with, then you basically to do a double
conditional: dob is optional, and stringFromDate returns optional, right? I'd
do it like this:
let dobString: String = {
guard
let dob = dob,
let dobString = serverDateFormatter.stringFromDate(dob)
else { return "" }
return dobString
}()
But if serverDateFormatter is actually a NSDateFormatter instance, which does
not return optionals, then you'd want to go with:
let dobString: String = {
guard let dob = dob else { return "" }
return serverDateFormatter.string(from:dob)
}()
-- E
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users