0

I have this code in my project:

class ShippingDay: EVObject, Mappable {
    var periods: [String]?
    var date: Date?

    required init(map: Mapper) throws {
        self.periods = map.optionalFrom("periods")
        if let dateStr:String = map.optionalFrom("date") {
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy/MM/dd"
            //formatter.locale = Locale(identifier: "en_US_POSIX")
            //formatter.calendar = Calendar(identifier: .gregorian)
            self.date = formatter.date(from: dateStr)
        }
    }

here is the theoretical proof in playground that this should work:

enter image description here

But this is what I get:

enter image description here

Can some enlightened soul please explain me what the hell is happening here?? Why my self.date is always nil? I tried without the comments and the result is the same... nil

UPDATED evidence:

enter image description here

rickrvo
  • 543
  • 3
  • 17
  • set locale to formatter formater.locale = Locale(identifier: "en_US_POSIX") – Dilan Mar 12 '20 at 14:21
  • I did... same result – rickrvo Mar 12 '20 at 14:23
  • I tried setting locale, calendar and timezone... I always get nil and they work fine on playground – rickrvo Mar 12 '20 at 14:25
  • what is the format of your dateStr? i mean yyyy-MM-dd type – Dilan Mar 12 '20 at 14:26
  • yyyy/MM/dd and you can see it's value is "2020/03/26" – rickrvo Mar 12 '20 at 14:27
  • I'm going nuts here... it doesn't make any sense... my mac is near to flying out of the window... – rickrvo Mar 12 '20 at 14:33
  • Consider that in a few countries daylight saving time changes on 2020/03/27 at midnight, so there is no date between `0:00:00` and `0:59:59` – vadian Mar 12 '20 at 14:33
  • its giving me nil for the entire array going from 2020/03/20 to 2020/03/30 – rickrvo Mar 12 '20 at 14:34
  • I tried your code in my Xcode ,it works, May be something in your xcode,emulator,mac setting – Dilan Mar 12 '20 at 14:35
  • 1
    Add `formater.locale = Locale(identifier: "en_US_POSIX")` again and put it **before** the line to set the date format. – vadian Mar 12 '20 at 14:36
  • just tried that also... same result :'( – rickrvo Mar 12 '20 at 14:42
  • I think this is a xcode bug... I'm on version Version 11.3.1 (11C504) – rickrvo Mar 12 '20 at 14:43
  • @rickrvo you need to set the dateFormatter Locale before setting the dateFormat. Btw forget about `Mappable` and start using `Codable` protocol – Leo Dabus Mar 12 '20 at 14:44
  • @LeoDabus I tried that and it's the same result – rickrvo Mar 12 '20 at 14:46
  • 1
    Are you sure the date is nil? Try asking for it as NSDate. There is a bug in lldb that reports nonnil Date objects as nil. But for NSDate you get the truth. See https://stackoverflow.com/questions/58108824/instantiated-optional-variable-shows-as-nil-in-xcode-debugger – matt Mar 12 '20 at 14:46
  • @matt like -> self.date = formatter.date(from: dateStr) as NSDate and leave the line var date:Date? untouched? – rickrvo Mar 12 '20 at 14:48
  • @rickrvo make sure to set the calendar as well if you don't provide the time on your string. https://stackoverflow.com/a/32408916/2303865 – Leo Dabus Mar 12 '20 at 14:49
  • @rickrvo Does it fail only with this specific date? – Leo Dabus Mar 12 '20 at 14:51
  • fails with all dates – rickrvo Mar 12 '20 at 14:53
  • Again just forget about Mappable – Leo Dabus Mar 12 '20 at 14:56
  • I'll update my question in a sec with the print for locale, calendar and timezone set – rickrvo Mar 12 '20 at 14:56
  • 1
    Try it without Mappable. Don't waste your time with it – Leo Dabus Mar 12 '20 at 14:57
  • yeah I know it's an old and huge project I haven't had the time to update it the mappable thing... but there are other objects that do parse date correctly – rickrvo Mar 12 '20 at 15:05
  • @matt you sir saved my sanity! changing the type to NSDate did the trick! not just lldb that leaves it nil, it really leaves the Date object nil – rickrvo Mar 12 '20 at 15:14
  • As the dupe answer points out, the problem is not the `DateFormatter` or `Date`, but a debugger bug that reports `nil` when it’s not. Don’t use `NSDate` because of some debugger idiosyncrasy. – Rob Mar 12 '20 at 15:31
  • @Rob it´s not just the debugger. I think... elsewhere in the code I see nill in other Date properties parsed from this object – rickrvo Mar 12 '20 at 16:34

0 Answers0