Coder Social home page Coder Social logo

yncodable's Introduction

YNCodable

YNCodable是基于swift4新特性Codable封装的一行代码实现json与model的映射,如果你还不了解Codable新特性,可以查阅官方文档

使用

1. 常规json串

let response = """
                {
                 "list": [
                    {
                      "title": "title1 ",
                      "series": "series"
                    },
                    {
                      "title": "title2"
                    }
                 ]
                }
               """

该json串中list数组有两个字段,字段title均有值,字段series可能没有值。我们可以定义如下Model:

struct ResponseList: Codable {
  var list: [List] = [List]()
}

struct List: Codable {
  var title: String = ""
  var series: String?
}

注意这里series是optional, 我们假定服务端是不靠谱的,所以所有字段都应该是optional,或者有默认值。

使用YNCodable解析

let responseList = ResponseList.decode(from: response)

其实这里也可以这么解析

let list = [List].decode(from: response)

2. 带keyPath的json串

很多时候服务器返回的json串往往不是直接解析,需要取一层,有时候可能还要取多层,YNCodable提供了带keyPath的解析方式。如下json:

let responseRes = """
                  {
                    "test": {
                      "res": {
                          "list": [
                              {
                                "title": "title1 ",
                                "series": "series"
                              },
                              {
                                "title": "title2"
                              }
                          ]
                        }
                      }
                    }
               """

这里还是解析list

let responseListFromKeypath = ResponseList.decode(from: responseRes, keyPath: "test.res")

或者直接返回数组

listFromKeyPath = [List].decode(from: responseRes, keyPath: "test.res.list")

要求

  • Xcode9
  • swift

安装

YNCodable不包含任何依赖

CocoaPods

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
	pod 'YNCodable'
end

在Podfile文件中替换 YOUR_TARGET_NAME 进入到项目目录执行

$ pod install

yncodable's People

Contributors

huangyanan1992 avatar

Stargazers

 avatar  avatar  avatar 公子荣 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.