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

2016-08-04 Thread Zhao Xin via 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
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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

2016-08-04 Thread Erica Sadun via swift-users

> On Aug 4, 2016, at 1:41 PM, Tim Vermeulen via swift-users 
>  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


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

2016-08-04 Thread Erica Sadun via swift-users

> On Aug 4, 2016, at 11:32 AM, Daniel Tartaglia via swift-users 
>  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


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

2016-08-04 Thread Daniel Tartaglia via swift-users
Thanks Tim!

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

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


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

2016-08-04 Thread Saagar Jha via 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  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 > > wrote:
>> 
>> That’s not possible. stringFromDate requires an NSDate, but dob is an 
>> optional
>> 
>>> On Aug 4, 2016, at 2:59 PM, Saagar Jha >> > 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 > 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 
 > wrote:
 That’s where I would use the ?? operator:
 
 let dobString = serverDateFormatter.stringFromDate(dob) ?? ""
 
 
 Jeff Kelley
 
 slauncha...@gmail.com  | @SlaunchaMan 
  | jeffkelley.org 
> On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users 
> > 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 
 
 
 
 ___
 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] Cleaner way than if let initialization?

2016-08-04 Thread Tim Vermeulen via swift-users
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?
> 
> 
> 
> 
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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

2016-08-04 Thread Daniel Tartaglia via swift-users
That’s not possible. stringFromDate requires an NSDate, but dob is an 
optional

> On Aug 4, 2016, at 2:59 PM, Saagar Jha  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 > > 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 
>> > wrote:
>> That’s where I would use the ?? operator:
>> 
>> let dobString = serverDateFormatter.stringFromDate(dob) ?? ""
>> 
>> 
>> Jeff Kelley
>> 
>> slauncha...@gmail.com  | @SlaunchaMan 
>>  | jeffkelley.org 
>>> On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users 
>>> > 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 
>> 
>> 
>> 
>> ___
>> 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


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

2016-08-04 Thread Saagar Jha via 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  
> 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 
> > wrote:
> That’s where I would use the ?? operator:
> 
> let dobString = serverDateFormatter.stringFromDate(dob) ?? ""
> 
> 
> Jeff Kelley
> 
> slauncha...@gmail.com  | @SlaunchaMan 
>  | jeffkelley.org 
>> On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users 
>> > 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 
> 
> 
> 
> ___
> 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


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

2016-08-04 Thread Zhao Xin via 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
>
> slauncha...@gmail.com | @SlaunchaMan  |
> 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
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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

2016-08-04 Thread Jeff Kelley via swift-users
That’s where I would use the ?? operator:

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


Jeff Kelley

slauncha...@gmail.com | @SlaunchaMan  | 
jeffkelley.org 
> On Aug 4, 2016, at 1:32 PM, Daniel Tartaglia via swift-users 
>  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


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

2016-08-04 Thread Daniel Tartaglia via swift-users
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