栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Swift4中使用Codable将日期字符串转换为可选的小数秒

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Swift4中使用Codable将日期字符串转换为可选的小数秒

您可以使用两个不同的日期格式器(带和不带小数秒)并创建自定义的DateDecodingStrategy。如果在解析API返回的日期时失败,则可以按@PauloMattos的建议在注释中引发DecodingError:

iOS 9,macOS 10.9,tvOS 9,watchOS 2,Xpre 9或更高版本

自定义ISO8601DateFormatter:

extension Formatter {    static let iso8601withFractionalSeconds: DateFormatter = {        let formatter = DateFormatter()        formatter.calendar = Calendar(identifier: .iso8601)        formatter.locale = Locale(identifier: "en_US_POSIX")        formatter.timeZone = TimeZone(secondsFromGMT: 0)        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"        return formatter    }()    static let iso8601: DateFormatter = {        let formatter = DateFormatter()        formatter.calendar = Calendar(identifier: .iso8601)        formatter.locale = Locale(identifier: "en_US_POSIX")        formatter.timeZone = TimeZone(secondsFromGMT: 0)        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX"        return formatter    }()}

习惯

DateDecodingStrategy

extension JSONDeprer.DateDecodingStrategy {    static let customISO8601 = custom {        let container = try $0.singlevalueContainer()        let string = try container.depre(String.self)        if let date = Formatter.iso8601withFractionalSeconds.date(from: string) ?? Formatter.iso8601.date(from: string) { return date        }        throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid date: (string)")    }}

习惯

DateEncodingStrategy

extension JSONEnprer.DateEncodingStrategy {    static let customISO8601 = custom {        var container = $1.singlevalueContainer()        try container.enpre(Formatter.iso8601withFractionalSeconds.string(from: $0))    }}

编辑/更新

Xpre 10•Swift 4.2或更高版本•iOS 11.2.1或更高版本

ISO8601DateFormatter
现在支持
formatOptions
.withFractionalSeconds

extension Formatter {    static let iso8601withFractionalSeconds: ISO8601DateFormatter = {        let formatter = ISO8601DateFormatter()        formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]        return formatter    }()    static let iso8601: ISO8601DateFormatter = {        let formatter = ISO8601DateFormatter()        formatter.formatOptions = [.withInternetDateTime]        return formatter    }()}

习惯

DateDecodingStrategy
DateEncodingStrategy
将与上面显示的相同。


// Playground testingstruct ISODates: Codable {    let dateWith9FS: Date    let dateWith3FS: Date    let dateWith2FS: Date    let dateWithoutFS: Date}

let isoDatesJSON = """{"dateWith9FS": "2017-06-19T18:43:19.532123456Z","dateWith3FS": "2017-06-19T18:43:19.532Z","dateWith2FS": "2017-06-19T18:43:19.53Z","dateWithoutFS": "2017-06-19T18:43:19Z",}"""

let isoDatesData = Data(isoDatesJSON.utf8)let deprer = JSonDeprer()deprer.dateDecodingStrategy = .customISO8601do {    let isoDates = try deprer.depre(ISODates.self, from: isoDatesData)    print(Formatter.iso8601withFractionalSeconds.string(from: isoDates.dateWith9FS))   // 2017-06-19T18:43:19.532Z    print(Formatter.iso8601withFractionalSeconds.string(from: isoDates.dateWith3FS))   // 2017-06-19T18:43:19.532Z    print(Formatter.iso8601withFractionalSeconds.string(from: isoDates.dateWith2FS))   // 2017-06-19T18:43:19.530Z    print(Formatter.iso8601withFractionalSeconds.string(from: isoDates.dateWithoutFS)) // 2017-06-19T18:43:19.000Z} catch {    print(error)}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号