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

Saagar Jha saagar at saagarjha.com
Thu Aug 4 13:59:53 CDT 2016


As such, there shouldn’t be a need for an if; `let dobString = serverDateFormatter.stringFromDate(dob)` should be sufficient.

Saagar Jha



> On Aug 4, 2016, at 11:55, Zhao Xin via swift-users <swift-users at swift.org> wrote:
> 
> It can't be done by ?? as stringFromDate(Date) returns String instead of String?
> 
> Zhaoxin
> 
> 
> On Fri, Aug 5, 2016 at 1:42 AM, Jeff Kelley via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> That’s where I would use the ?? operator:
> 
> let dobString = serverDateFormatter.stringFromDate(dob) ?? ""
> 
> 
> Jeff Kelley
> 
> SlaunchaMan at gmail.com <mailto:SlaunchaMan at gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan> | jeffkelley.org <http://jeffkelley.org/>
>> On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users <swift-users at swift.org <mailto:swift-users at 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?
>> 
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> 
> _______________________________________________
> 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/20160804/55a8afc3/attachment.html>


More information about the swift-users mailing list