当前位置:网站首页>Use of swift basic codable (jsonencoder jsondecoder)
Use of swift basic codable (jsonencoder jsondecoder)
2022-07-25 19:13:00 【Feng Hanxu】
I always feel that what I write is not technology , But feelings , One tutorial after another is the trace of one's own journey . Success with expertise is the most replicable , I hope my path will make you less detours , I hope I can help you erase the dust of knowledge , I hope I can help you clarify the context of knowledge , I hope there will be you and me on the top of technology in the future .
Preface
Codable The use of is generally used to store the data of the model , The model passes before storage Codable convert to data, Then take it out and pass data Convert to the corresponding model ,2. Add the use of route jump controller to change the model from A The controller is passed to B controller . It needs to be converted into binary . Restore the model after passing it back
On the subject
Use Codable Model of , The model needs to be followed Codable agreement .
If the model is nested with the model , Every model should also follow Codable agreement
for instance :

import Foundation
enum TimelineMediaType: Int, Codable {
case unknown
case images
case video
}

import Foundation
public enum UserStatus: Int, Codable {
case none = 0 // Unknown
case free = 1 // Free
case calling = 2 // Talking about
case resting = 3 // Don't disturb
case hide = 4 // stealth
case offline = 6 // offline
public static func fromInt(_ value: Int) -> UserStatus {
switch value {
case 1: return .free
case 2: return .calling
case 3: return .resting
case 4, 5: return .hide
case 6, 7: return .offline
default: return .none
}
}
public var description: String {
switch self {
case .none: return ""
case .free: return " Free "
case .calling: return " Talking about "
case .resting: return " Don't disturb "
case .hide: return " stealth "
case .offline: return " offline "
}
}
}

import Foundation
struct GreetingSelectionCache: Codable {
var textID: Int64 = 0
var voiceID: Int64 = 0
var pictureID: Int64 = 0
}

import Foundation
class TimelineDetailModel: Codable {
var timelineDetailType: TimelineMediaType
var onlineState: UserStatus
var title: String
var cache: GreetingSelectionCache
init(
_ timelineDetailType: TimelineMediaType = .unknown,
_ onlineState: UserStatus = .none,
_ title: String = "",
_ cache: GreetingSelectionCache = GreetingSelectionCache(textID: 0, voiceID: 0, pictureID: 0)
) {
self.timelineDetailType = timelineDetailType
self.onlineState = onlineState
self.title = title
self.cache = cache
}
}
Use :
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let JSONEncoderModel = TimelineDetailModel()
JSONEncoderModel.timelineDetailType = .images
JSONEncoderModel.onlineState = .calling
JSONEncoderModel.title = "fenghanxu"
let cache = GreetingSelectionCache(textID: 1, voiceID: 2, pictureID: 3)
JSONEncoderModel.cache = cache
guard let data = try? JSONEncoder().encode(JSONEncoderModel) else {
return }
let JSONDecoderModel = try? JSONDecoder().decode(TimelineDetailModel.self, from: data)
print("timelineDetailType: \(String(describing: JSONDecoderModel?.timelineDetailType))")
print("onlineState: \(String(describing: JSONDecoderModel?.onlineState))")
print("title: \(String(describing: JSONDecoderModel?.title))")
print("cache: \(String(describing: JSONDecoderModel?.cache))")
}
Print the results :
timelineDetailType: Optional(SwiftDemol.TimelineMediaType.images)
onlineState: Optional(SwiftDemol.UserStatus.calling)
title: Optional("fenghanxu")
cache: Optional(SwiftDemol.GreetingSelectionCache(textID: 1, voiceID: 2, pictureID: 3))
边栏推荐
- 无惧高温暴雨,有孚网络如何保您无忧?
- In the first half of the year, the shipment volume has exceeded that of the whole year of last year, and centritec millimeter wave radar has "captured" the international giant
- SQL Server 2019 installation tutorial
- 阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?
- HTTP cache tongtianpian, there may be something you want
- Pixel2mesh generates 3D meshes from a single RGB image eccv2018
- Interface automation test platform fasterrunner series (II) - function module
- i3-status 配置
- JS basic type reference type deep / shallow clone copy
- How to prohibit the use of 360 browser (how to disable the built-in browser)
猜你喜欢

CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...

QIIME2得到PICRUSt2结果后如何分析

小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能
![[iniparser] simple use of the project configuration tool iniparser](/img/2b/1d20b4ef44dfe2544891d9c72b676e.png)
[iniparser] simple use of the project configuration tool iniparser

华为交换机系统软件升级和安全漏洞修复教程

【阅读笔记】《深度学习》第一章:引言

【iniparser】项目配置工具iniparser的简单使用

微信小程序 29 热搜榜的完善②

乐理基础 调式

Analysis of the internet jam in IM development? Network disconnection?
随机推荐
MySQL sub query (selected 20 sub query exercises)
小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
[cloud native kubernetes] management of secret storage objects under kubernetes cluster
Fearless of high temperature and rainstorm, how can Youfu network protect you from worry?
浅析IM即时通讯开发出现上网卡顿?网络掉线?
【小程序开发】宿主环境详解
SQL Server 2019 安装教程
The degree of interval of basic music theory
srec_ Use of common cat parameters
Gan, why ".Length! == 3??
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
Fearless of high temperature and rainstorm, how can Youfu network protect you from worry?
Fruit chain "siege": it's a journey of sweetness and bitterness next to apples
QIIME2得到PICRUSt2结果后如何分析
kubernetes RBAC
【Web技术】1391- 页面可视化搭建工具前生今世
Full scale and Xuan of C key
小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
Summer Challenge [FFH] this midsummer, a "cool" code rain!
Based on easycv to reproduce Detr and dab-detr, the correct opening method of object query