Swift: NSDateFormatter dateFromString returns nil on devices

后端 未结 1 842
无人及你
无人及你 2021-01-05 09:47

I found something that could solve my problem in Obj-c, but I\'m not able to translate the code, so I\'m asking for a Swift solution.

I\'m parsing some data from a J

相关标签:
1条回答
  • 2021-01-05 10:39

    For the others who get here and don't read the comments of the initial post ;)

    As @rintaro pointed out - and what was my problem too - you have to add a locale when you use fixed-format dates:

    If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW7

    So in Swift 3 you would have to add

    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    
    0 讨论(0)
提交回复
热议问题