📂
JackyChen的精神時光屋
  • About
  • iPlayground
    • iPlayground 2020
      • iPlayground submit 2020
    • iPlayground 2019
      • Untitled
      • iPlayground submit 2019
  • WWDC
    • 2020 WWDC
    • 2019 WWDC
    • 2018 WWDC
      • What's New in Testing
      • BusinessChat
    • 2016 WWDC
      • What's New in the Apple Push Notification Service
  • AR/VR
    • ARKit plugin at Unity
    • ARKit
    • AR/VR 實習作品分享
    • Google Blocks
  • CI/CD
    • Continous Integretion for Unity
    • 拯救地球大作戰-自動化設定注意事項
    • Provisioning Profile 自動化更新
    • Make ipa file with personal team of code sign
    • Xcode11 版號問題
  • Test
    • Cucumberish
    • XCUITest
    • Design Patterns in XCUITest
    • Unit Test
  • User Interface
    • IBDesignable 和 IBInspectable
    • iOS 使用貝塞爾曲線繪製路徑
    • UIStatusBarStyle
    • iOS Devices Specification
    • Vector Image
    • Launch Screen
    • Haptic Feedback
    • Good Works for Storyboard
    • Cell 展開收合效果
    • ScrollView
    • Swift lazy
    • Lottie
  • Foundation
    • Adding a Custom Font to Your App
    • WKWebView how to work with javascript
    • Global Central Dispatch
    • HealthKit
    • Error Handling
    • Debug with LLDB
    • Application Lifecycle
    • Swift Codable
    • Push Notifications
    • AVFoudation
  • Others
    • C語言指標概念
    • UnsafePointer(Swift)
    • iOS News Reference
    • Blender
    • Free Web Server
    • Firebase
    • Firebase migration
    • GraphQL
    • Ruby
    • zsh command line
    • visudo
  • Security
    • 課程:App資安規劃與實作
    • KeyChain
    • iOS反組譯程序
    • Arxan
  • Git
    • Git
    • xcodeproj 合併衝突
    • Pull Request
  • Machine Learning
    • CoreML
    • Vision
    • Turi Create
  • 待分類
    • ABI Stability and More
    • Mirror
    • Carthage
    • SwiftUI
    • MVVM
    • OpenSSL
    • USDZ Convert
    • Nexus repository and gitlfs
Powered by GitBook
On this page

Was this helpful?

  1. Foundation

Swift Codable

PreviousApplication LifecycleNextPush Notifications

Last updated 5 years ago

Was this helpful?

import Foundation

let jsonString = """
{
    "title": "Blah",
    "age": 12,
    "gender": "F",
    "body":{
        "weight": 60,
        "height": 180
    }
}
"""

struct Gender
{
    var sex: String!
}

struct Person: Codable
{
    // 一般配對
    var name: String!
    var age: Int!

    // JsonString->Object 一層變兩層
    var gender: Gender! = Gender()

    // JsonString->Object 兩層變一層
    var weight: Int!
    var height: Int!

    enum CodingKeys: String, CodingKey
    {
        case name = "title"
        case age

        case gender

        case body
    }

    enum BodyKeys: String, CodingKey
    {
        case weight
        case height
    }

    init(from decoder: Decoder) throws
    {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(String.self, forKey: .name)
        age = try container.decode(Int.self, forKey: .age)

        gender.sex = try container.decode(String.self, forKey: .gender)

        let body = try container.nestedContainer(keyedBy: BodyKeys.self, forKey: .body)
        weight = try body.decode(Int.self, forKey: .weight)
        height = try body.decode(Int.self, forKey: .height)
    }

    func encode(to encoder: Encoder) throws
    {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
        try container.encode(age, forKey: .age)

        try container.encode(gender.sex, forKey: .gender)

        var body = container.nestedContainer(keyedBy: BodyKeys.self, forKey: .body)
        try body.encode(weight, forKey: .weight)
        try body.encode(height, forKey: .height)
    }
}
  • 結果

用Codable協議實現快速JSON解析