美文网首页
网络请求:Alamfire+数据模型

网络请求:Alamfire+数据模型

作者: iOS分享 | 来源:发表于2019-07-19 14:50 被阅读0次

    首先在Podfiled中导入三个框架

     pod 'Alamofire'
     pod 'SwiftyJSON'
     pod 'HandyJSON'
    

    创建名为NetworkTool的swift文件,进行网络请求

    import UIKit
    import Alamofire
    import SwiftyJSON
    //import SwiftyJSON
    
    protocol NetworkToolProtocol {
      //-------------------我的 Mine ------------------
      //我的界面Cell的数据
      static func loadMyCellData(completionHandler:@escaping (_ sections:[[MyCellModel]])->())
      //我的关注的数据
    
    extension NetworkToolProtocol{
      //我的界面cell的数据请求
      static func loadMyCellData(completionHandler:@escaping (_ sections:[[MyCellModel]])->()){
          let url = BASE_URL + "/user/tab/tabs/?"
          let params = ["device_id":device_id]
          Alamofire.request(url,parameters:params).responseJSON { (response) in
              //判断网络请求是否成功
              guard response.result.isSuccess else{
                  //网络错误的提示信息
                  return
              }
              if let value = response.result.value{
                  //JSON  SwiftyJSON语句
                  let json = JSON(value)
                  guard json["message"] == "success" else{return}
                  if let data = json["data"].dictionary{
                      print(data)
                      if let sections = data["sections"]?.array{
                          var sectionArray = [[MyCellModel]]()
                          for item in sections{
                              var rows = [MyCellModel]()
                              for row in item.arrayObject!{
                                  let myCellModel = MyCellModel.deserialize(from: row as? NSDictionary)
                                  rows.append(myCellModel!)
                              }
                              sectionArray.append(rows)
                          }
                          completionHandler(sectionArray)
                      }
              }
          }
      }
      }
    }
    struct NetworkTool:NetworkToolProtocol {}
    

    获取数据JSON图:

    {
        "data": {
            "sections": [
                [
                    {
                        "text": "我的钱包",
                        "grey_text": "全民抽奖7月好礼",
                        "url": "sslocal://webview?url=https%3a%2f%2fis.snssdk.com%2ffeoffline%2fwallet_portal%2findex.html&title=%E6%88%91%E7%9A%84%E9%92%B1%E5%8C%85&hide_more=1&hide_bar=1&bounce_disable=1&hide_status_bar=1&back_button_color=white&status_bar_color=white&background_colorkey=3&should_append_common_param=1&disable_web_progressView=1&use_offline=1&show_load_anim=0",
                        "key": "mall",
                        "icons": {
                            "day": {
                                "height": 24,
                                "radius": 0,
                                "uri": "",
                                "url_list": [
                                    {
                                        "url": "http://p3.pstatp.com/origin/1e0660002e8c1d26332ce"
                                    }
                                ],
                                "width": 24
                            },
                            "night": {
                                "height": 24,
                                "radius": 0,
                                "uri": "",
                                "url_list": [
                                    {
                                        "url": "http://p3.pstatp.com/origin/1e0660002e8c1d26332ce"
                                    }
                                ],
                                "width": 24
                            }
                        },
                        "tip_new": 0,
                        "tip_text": ""
                    },
                    {...}
                ],
                [...],
                [...]
            ]
        },
        "message": "success"
    }
    

    创建一个名为MyCellModel的数据模型文件,来对应获取的数据。

    import HandyJSON
    struct MyCellModel:HandyJSON {
        var grey_text:String = ""
        var text:String = ""
        var url:String = ""
        var key:String = ""
        var tip_new:Int = 0
        }
    }
    

    最后在需要获取数据的地方调用此代码

      NetworkTool.loadMyCellData(completionHandler: )
    

    相关文章

      网友评论

          本文标题:网络请求:Alamfire+数据模型

          本文链接:https://www.haomeiwen.com/subject/epcclctx.html