String extensions toDate() is crash some device

Two solutions:

  1. Add en_US_POSIX locale for a fixed format.
  2. Use ISO8601DateFormatter (iOS 8+), the benefit is no locale, no date format, no time zone.

    func toDate() -> Date? {
        let dateFormatter = ISO8601DateFormatter()
        return dateFormatter.date(from: self) 
    }
    

In any case return an optional, fatalError() in production environment causes pretty bad user experience.

Leave a Comment