欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > Swift 基于Codable协议使用

Swift 基于Codable协议使用

2024/10/24 22:24:40 来源:https://blog.csdn.net/bobbob32/article/details/140298519  浏览:    关键词:Swift 基于Codable协议使用

Codable协议 继承自 Decodable & Encodable

//
// Test1.swift
// TestDemo
//
// Created by admin on 2024/7/9.
//

import Foundationstruct Player{var name:Stringvar highScore:Int = 0var history:[Int] = []var address:Address?var birthday:Date?init(name: String) {self.name = name}}extension Player{mutating func updateScore(_ newScore:Int){history.append(newScore)if(highScore < newScore){highScore = newScore}}
}

//Codable 继承自 Decodable & Encodable
/*

Codable 是 Swift 中用于简化对象序列化和反序列化的协议组合。它由 Encodable 和 Decodable 组成,分别用于将对象编码为外部表示(如 JSON)和从外部表示解码对象。通过实现 Codable,我们可以轻松地将自定义类型与 JSON 等格式进行转换。

*/

extension Player:Codable{/*如果 JSON 中的键与结构体中的属性名称不一致,可以使用 CodingKeys 枚举来定义自定义的键映射*/enum CodingKeys:String,CodingKey{case namecase highScorecase addresscase birthdaycase history = "history1"}
}

/*
Codable 同样适用于嵌套类型
*/

struct Address:Codable{var street:Stringvar citty:Stringvar zipCode:String
}
func test111(){var player = Player(name: "John")player.address = Address(street: "宝田一路", citty: "深圳", zipCode: "2212")player.birthday = Date(timeIntervalSince1970: 25 * 365 * 24 * 60 * 60)player.updateScore(121)player.updateScore(134)/*默认情况下,JSONEncoder 和 JSONDecoder 对日期的处理方式可能不符合需求,可以自定义日期格式*/let formatter = DateFormatter()formatter.dateFormat = "yyyy-MM-dd"let encoder = JSONEncoder()encoder.dateEncodingStrategy = .formatted(formatter)let jsonData = try? encoder.encode(player)guard let jsonData = jsonData else{print("json data is nil")return}let jsonStr = String(data: jsonData, encoding: .utf8)guard let jsonStr = jsonStr else{print("json 数据编码失败")return}print("player 转json 字符串 : " + jsonStr)let decoder = JSONDecoder()decoder.dateDecodingStrategy = .formatted(formatter)guard let jsonDataDe = jsonStr.data(using: .utf8) else {print("json DataDe is null")return}//解码异常处理和打印var user :Player?do {user = try decoder.decode(Player.self, from: jsonDataDe)} catch let DecodingError.dataCorrupted(context) {print(context)} catch let DecodingError.keyNotFound(key, context) {print("Key '\(key)' not found:", context.debugDescription)print("codingPath:", context.codingPath)} catch let DecodingError.valueNotFound(value, context) {print("Value '\(value)' not found:", context.debugDescription)print("codingPath:", context.codingPath)} catch let DecodingError.typeMismatch(type, context)  {print("Type '\(type)' mismatch:", context.debugDescription)print("codingPath:", context.codingPath)} catch {print("Error: ", error)}guard let user = user else{print("user is null")return}print("name:" + user.name + ",score:" + "\(user.highScore)")}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com