Giter Site home page Giter Site logo

seasonzhu / alamofirecodable Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 0.0 186 KB

通过Alamofire请求数据,将数据一步到位转为基于遵守Codable协议的模型.同时包含使用coobjc

License: MIT License

Ruby 7.29% Objective-C 4.52% Swift 88.19%
alamofire codable extension json model ios

alamofirecodable's Introduction

AlamofireCodable

通过Alamofire请求数据,将数据一步到位转为基于遵守Codable协议的模型!

使用原生的Codable协议进行JSON转模型!

例如有下面这样的JSON:

{
  "code": 0,
  "list": [
    {
      "topicDesc": "错过线下行前说明会?这里有一份超完整的澳洲行前攻略给你。",
      "createTime": null,
      "topicImageUrl": "http:\\/\\/cdn.timez1.com\\/20180608\\/18626cd2106344078969afd77acf3572.jpg",
      "id": 12,
      "topicStatus": 1,
      "upTime": "2018-06-13 14:46:06",
      "topicOrder": 21,
      "topicTittle": "澳洲行前"
    },
    {
      "topicDesc": "错过线下行前说明会?这里有一份超完整的英国行前攻略给你。",
      "createTime": null,
      "topicImageUrl": "http:\\/\\/cdn.timez1.com\\/20180608\\/d27a7923a24d4cd6878fe08fa338be45.jpg",
      "id": 10,
      "topicStatus": 1,
      "upTime": "2018-06-13 14:46:17",
      "topicOrder": 20,
      "topicTittle": "英国行前"
    },
    {
      "topicDesc": "每月准时更新!留学时区为你搜集6月份留学活动资讯,想参加活动就来吧~",
      "createTime": null,
      "topicImageUrl": "http:\\/\\/cdn.timez1.com\\/20180613\\/ad2983334ede4b18a0ac39e2af2df7de.jpg",
      "id": 13,
      "topicStatus": 1,
      "upTime": "2018-06-13 14:46:34",
      "topicOrder": 19,
      "topicTittle": "活动推荐"
    },
    {
      "topicDesc": "英国留学 - 5分钟语音微讲座",
      "createTime": null,
      "topicImageUrl": "http:\\/\\/cdn.timez1.com\\/20180507\\/de7598df27a048949d7108f084156f2e.jpg",
      "id": 8,
      "topicStatus": 1,
      "upTime": "2018-05-29 15:42:44",
      "topicOrder": 17,
      "topicTittle": "英国语音"
    },
    {
      "topicDesc": "美国留学 - 5分钟语音微讲座",
      "createTime": null,
      "topicImageUrl": "http:\\/\\/cdn.timez1.com\\/20180507\\/c052c8d666d54533aa823cf7991f83b6.jpg",
      "id": 7,
      "topicStatus": 1,
      "upTime": "2018-05-29 15:42:37",
      "topicOrder": 16,
      "topicTittle": "美国语音"
    },
    {
      "topicDesc": "澳大利亚留学 - 5分钟语音微讲座",
      "createTime": null,
      "topicImageUrl": "http:\\/\\/cdn.timez1.com\\/20180507\\/67189c3eb55b4ead98952c66722a0515.jpg",
      "id": 6,
      "topicStatus": 1,
      "upTime": "2018-06-25 10:48:41",
      "topicOrder": 15,
      "topicTittle": "澳洲语音"
    }
  ]
}

可以通过一些网站获取其模型,我们让每一个自定义的模型遵守Coable协议,如下:

struct Item: Codable {
    var topicOrder: Int?
    var id: Int?
    var topicDesc: String?
    var topicTittle: String?
    var upTime: String?
    var topicImageUrl: String?
    var topicStatus: Int?
}

struct Topics: Codable {
    var list: [Item]?
    var code: Int?
}

我们就可以通过Alamofire和我写一个分类方法responseCodable获取其模型了:

// 获取整个模型
Alamofire.request("http://sun.topray-media.cn/tz_inf/api/topics", method: .post).responseCodable { (response: DataResponse<Topics>) in
	guard let value = response.value else { return }
  	print(value)
}

当然我们也可以通过其keyPath获取其更底层的模型:

// keyPath目标的请求Model
Alamofire.request("http://sun.topray-media.cn/tz_inf/api/topics", method: .post).responseCodable(queue: nil, keyPath: "list") { (response: DataResponse<[Item]>) in
	guard let list = response.value else { return }
	print(list)
}

AlamofireCodable的优势

  1. 完全使用系统协议,不依赖于其他JSON转模型的框架,就算不使用Alamofire,可以使用原生的请求一步到位获取模型.
  2. 不用区分响应的是Object或者是[Object],上面的例子已经可以看出.
  3. keyPath获取深层次的keyPath下的模型,可以在requestModel2的方法中看具体实现,而ObjectMapper只能获取一层下面的模型.
  4. 定义的模型简单,模型类或者结构体只需要遵守Codable协议即可.
  5. 涉及模型中定义自定义数据的,可以在Demo中Model2进行详细了解,这里只说重点,实现Decodable协议的具体方法!

pod使用注意

目前仅推到我自己的私有库,具体如何使用,可以参看该页面的使用说明. 至于原因,是有位大哥已经使用了AlamofireCodable在Cocoapods占了坑,那位大哥的写法借鉴了AlamofireObjectMapper.我没有办法推到Cocoapods上面去.

文章参考

Encoding and Decoding Custom Types 

Using JSON with Custom Types 

Demo地址

AlamofireCodable

AlamofireCoswift

2020年6月12日更新,今天看了看这个Demo,其实还有第二部,是关于coobjc+Alamofire的封装,天知道我之前都在写什么代码,反正使用了这个封装之后,就可以类似Python/Dart中使用await进行异步操作了,当然我太长时间没有看Alamofire的源码了,可能都需要同步更新一下才行.

19年都在全身心投入在Swift中,后面渐渐接触了Flutter与Python,了解到了编程世界的宽广,还需要继续努力.

alamofirecodable's People

Contributors

seasonzhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.