当前位置:网站首页>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))
边栏推荐
- Basic mode of music theory
- 华为交换机系统软件升级和安全漏洞修复教程
- iMeta | Sangerbox: 交互式整合临床生信分析平台
- Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!
- How to change the chords after the tune of the song is changed
- Hough transform understanding [easy to understand]
- FPGA based 1080p 60Hz bt1120 interface debugging process record
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
- 怎样设计产品帮助中心?以下几点不可忽视
- ThreadLocal Kills 11 consecutive questions
猜你喜欢

常用的开发软件下载地址

ThreadLocal Kills 11 consecutive questions

乐理基础 调式

阿里云免费SSL证书申请详细流程

FPGA based 1080p 60Hz bt1120 interface debugging process record

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

Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice

小程序毕设作品之微信校园维修报修小程序毕业设计成品(1)开发概要

Wechat campus maintenance and repair application applet graduation design finished product of applet completion work (6) opening defense ppt

CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
随机推荐
歌曲转调之后和弦如何转换
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
QIIME2得到PICRUSt2结果后如何分析
Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!
小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板
常用的开发软件下载地址
SQL realizes 10 common functions of Excel, with original interview questions attached
阿里云免费SSL证书申请详细流程
有孚原力超算,为客户提供定制化高性能计算服务
小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能
Single arm routing experiment demonstration (Huawei router device configuration)
Clip can also do segmentation tasks? The University of Gottingen proposed a model clipseg that uses text and image prompt and can do three segmentation tasks at the same time, squeezing out the clip a
李宏毅《机器学习》丨1. Introduction of this course(机器学习介绍)
Is Cinda securities a state-owned enterprise? Is it safe to open an account in Cinda securities?
CRM configuration item command notes
[iniparser] simple use of the project configuration tool iniparser
Baklib: make excellent product instruction manual
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
SQL Server 2019 安装教程
ThreadLocal Kills 11 consecutive questions