[Swift4] Allow for custom dateformatter to be used (#6672)

* Add a custom dateformat to be set

* Samples updated

* Update CodableHelper.swift

Change to correct format: decoder.dataDecodingStrategy = .base64

* Update CodableHelper.mustache

Add `decoder.dataDecodingStrategy = .base64` back

* Update CodableHelper.swift

Fix `decoder.dataDecodingStrategy = .base64`

* Update CodableHelper.swift

Fix `decoder.dataDecodingStrategy = .base64`
This commit is contained in:
Dawid van Graan 2017-11-13 16:15:43 +02:00 committed by William Cheng
parent b3133a2d74
commit 5af788cb9f
4 changed files with 36 additions and 12 deletions

View File

@ -11,14 +11,20 @@ public typealias EncodeResult = (data: Data?, error: Error?)
open class CodableHelper {
open static var dateformatter: DateFormatter?
open class func decode<T>(_ type: T.Type, from data: Data) -> (decodableObj: T?, error: Error?) where T : Decodable {
var returnedDecodable: T? = nil
var returnedError: Error? = nil
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
if let df = self.dateformatter {
decoder.dateDecodingStrategy = .formatted(df)
} else {
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
}
}
do {

View File

@ -11,14 +11,20 @@ public typealias EncodeResult = (data: Data?, error: Error?)
open class CodableHelper {
open static var dateformatter: DateFormatter?
open class func decode<T>(_ type: T.Type, from data: Data) -> (decodableObj: T?, error: Error?) where T : Decodable {
var returnedDecodable: T? = nil
var returnedError: Error? = nil
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
if let df = self.dateformatter {
decoder.dateDecodingStrategy = .formatted(df)
} else {
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
}
}
do {

View File

@ -11,14 +11,20 @@ public typealias EncodeResult = (data: Data?, error: Error?)
open class CodableHelper {
open static var dateformatter: DateFormatter?
open class func decode<T>(_ type: T.Type, from data: Data) -> (decodableObj: T?, error: Error?) where T : Decodable {
var returnedDecodable: T? = nil
var returnedError: Error? = nil
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
if let df = self.dateformatter {
decoder.dateDecodingStrategy = .formatted(df)
} else {
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
}
}
do {

View File

@ -11,14 +11,20 @@ public typealias EncodeResult = (data: Data?, error: Error?)
open class CodableHelper {
open static var dateformatter: DateFormatter?
open class func decode<T>(_ type: T.Type, from data: Data) -> (decodableObj: T?, error: Error?) where T : Decodable {
var returnedDecodable: T? = nil
var returnedError: Error? = nil
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
if let df = self.dateformatter {
decoder.dateDecodingStrategy = .formatted(df)
} else {
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
}
}
do {