Cleaner way than if let initialization?

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?

That’s where I would use the ?? operator:

let dobString = serverDateFormatter.stringFromDate(dob) ?? ""

Jeff Kelley

SlaunchaMan@gmail.com | @SlaunchaMan <https://twitter.com/SlaunchaMan&gt; | jeffkelley.org <http://jeffkelley.org/&gt;

···

On Aug 4, 2016, at 1:32 PM, 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?

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

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

···

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?

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?

You want `flatMap`:

let dobString = dob.flatMap(serverDateFormatter.stringFromDate)

Or if you want `dobString` to be non-optional:

let dobString = dob.flatMap(serverDateFormatter.stringFromDate) ?? “"

You can just use map here too, right?

let dobString2: String = dob.map(serverDateFormatter.string) ?? ""

I was a little surprised that this didn't need the rest of the selector signature but that's because parameter stripping and type matching, isn't it?

···

On Aug 4, 2016, at 1:41 PM, Tim Vermeulen via swift-users <swift-users@swift.org> wrote:

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?

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

And Greg from Omni Group has an even better solution for you. Since DateFormatter inherits from Formatter, you can use its `string(for:)` which accepts optionals:

let dobString3 = serverDateFormatter.string(for:dob) ?? ""

-- E

···

On Aug 4, 2016, at 3:17 PM, Erica Sadun via swift-users <swift-users@swift.org> wrote:

On Aug 4, 2016, at 1:41 PM, Tim Vermeulen via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> 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) ?? “"

You can just use map here too, right?

let dobString2: String = dob.map(serverDateFormatter.string) ?? ""

I was a little surprised that this didn't need the rest of the selector signature but that's because parameter stripping and type matching, isn't it?

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?

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

Thanks Tim!

···

On Aug 4, 2016, at 3:41 PM, Tim Vermeulen <tvermeulen@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?

This doesn’t work because `dob` is optional and `stringFromDate` only takes a non-optional date.

···

That’s where I would use the ?? operator:

let dobString = serverDateFormatter.stringFromDate(dob) ?? ""

Jeff Kelley

SlaunchaMan@gmail.com(mailto:SlaunchaMan@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@swift.org(mailto:swift-users@swift.org)>wrote:
> 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?
>
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org(mailto:swift-users@swift.org)
> https://lists.swift.org/mailman/listinfo/swift-users

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@swift.org> wrote:

That’s where I would use the ?? operator:

let dobString = serverDateFormatter.stringFromDate(dob) ?? ""

Jeff Kelley

SlaunchaMan@gmail.com | @SlaunchaMan <https://twitter.com/SlaunchaMan&gt; |
jeffkelley.org

On Aug 4, 2016, at 1:32 PM, 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?

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Thank you. I think this is the best I saw today.

Zhaoxin

···

On Fri, Aug 5, 2016 at 5:24 AM, Erica Sadun via swift-users < swift-users@swift.org> wrote:

And Greg from Omni Group has an even better solution for you. Since
DateFormatter inherits from Formatter, you can use its `string(for:)` which
accepts optionals:

let dobString3 = serverDateFormatter.string(for:dob) ?? ""

-- E

On Aug 4, 2016, at 3:17 PM, Erica Sadun via swift-users < > swift-users@swift.org> wrote:

On Aug 4, 2016, at 1:41 PM, Tim Vermeulen via swift-users < > swift-users@swift.org> 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) ?? “"

You can just use map here too, right?

let dobString2: String = dob.map(serverDateFormatter.string) ?? ""

I was a little surprised that this didn't need the rest of the selector
signature but that's because parameter stripping and type matching, isn't
it?

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?

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

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@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@swift.org <mailto:swift-users@swift.org>> wrote:
That’s where I would use the ?? operator:

let dobString = serverDateFormatter.stringFromDate(dob) ?? ""

Jeff Kelley

SlaunchaMan@gmail.com <mailto:SlaunchaMan@gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan&gt; | jeffkelley.org <http://jeffkelley.org/&gt;

On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users <swift-users@swift.org <mailto: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?

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Ahh…then optional chaining is the way to go: `let dobString = serverDateFormatter?.stringFromDate(dob) ?? ""`

Saagar Jha

···

On Aug 4, 2016, at 12:41, Daniel Tartaglia <danielt1263@gmail.com> wrote:

That’s not possible. stringFromDate requires an NSDate, but dob is an optional<NSDate>

On Aug 4, 2016, at 2:59 PM, Saagar Jha <saagar@saagarjha.com <mailto:saagar@saagarjha.com>> wrote:

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@swift.org <mailto:swift-users@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@swift.org <mailto:swift-users@swift.org>> wrote:
That’s where I would use the ?? operator:

let dobString = serverDateFormatter.stringFromDate(dob) ?? ""

Jeff Kelley

SlaunchaMan@gmail.com <mailto:SlaunchaMan@gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan&gt; | jeffkelley.org <http://jeffkelley.org/&gt;

On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users <swift-users@swift.org <mailto: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?

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

Oh, wait, I can’t read. Ignore that; then there’s no easy way to do it.

Saagar Jha

···

On Aug 4, 2016, at 12:42, Saagar Jha <saagar@saagarjha.com> wrote:

Ahh…then optional chaining is the way to go: `let dobString = serverDateFormatter?.stringFromDate(dob) ?? ""`

Saagar Jha

On Aug 4, 2016, at 12:41, Daniel Tartaglia <danielt1263@gmail.com <mailto:danielt1263@gmail.com>> wrote:

That’s not possible. stringFromDate requires an NSDate, but dob is an optional<NSDate>

On Aug 4, 2016, at 2:59 PM, Saagar Jha <saagar@saagarjha.com <mailto:saagar@saagarjha.com>> wrote:

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@swift.org <mailto:swift-users@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@swift.org <mailto:swift-users@swift.org>> wrote:
That’s where I would use the ?? operator:

let dobString = serverDateFormatter.stringFromDate(dob) ?? ""

Jeff Kelley

SlaunchaMan@gmail.com <mailto:SlaunchaMan@gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan&gt; | jeffkelley.org <http://jeffkelley.org/&gt;

On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users <swift-users@swift.org <mailto: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?

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

That’s not possible. stringFromDate requires an NSDate, but dob is an optional<NSDate>

···

On Aug 4, 2016, at 2:59 PM, Saagar Jha <saagar@saagarjha.com> wrote:

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@swift.org <mailto:swift-users@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@swift.org <mailto:swift-users@swift.org>> wrote:
That’s where I would use the ?? operator:

let dobString = serverDateFormatter.stringFromDate(dob) ?? ""

Jeff Kelley

SlaunchaMan@gmail.com <mailto:SlaunchaMan@gmail.com> | @SlaunchaMan <https://twitter.com/SlaunchaMan&gt; | jeffkelley.org <http://jeffkelley.org/&gt;

On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users <swift-users@swift.org <mailto: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?

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users