swfit 开发学习笔记

针对常用第三方控件的使用:
上拉加载下拉刷新的使用(MJRefresh)
 

//Mark table的创建
dataSourceArray = NSMutableArray.init(capacity: 0)
        table.frame = CGRect(x: 0, y: 88, width: ScreenWidth, height: ScreenHeight - 88 - 49 - 100)
        table.delegate = self
        table.dataSource = self
        self.view.addSubview(table)
        
        table.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(refreshHeaderAction))
        table.mj_footer = MJRefreshAutoNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(footAction))
//刷新控件的暂停
     self.table.mj_header.endRefreshing()
     self.table.mj_footer.endRefreshing()

网络Alamofire的使用

Alamofire.request("http://www.yourUrl.com", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            
            switch response.result.isSuccess{
           
            case true:
                if let value = response.result.value{
                    print("\(String(describing: response.result.value))")
                    
                    let mappModel = Mapper().map(JSONObject: value)
                    print(String(describing: (mappModel?.data?.articleList![0] .ha_tags)))
                    
                    let json = JSON(value)
                    let model = ResponseBaseModel.init(jsonData : json)
                    let dic = JSON(model.data!).dictionaryValue
                    let valueModel = FineListModel.init(jsonData: JSON(dic))
                    let count = valueModel.articleList?.count
                    
                    let articleModel = valueModel.articleList?.first as! FineArticleListModel
                    print(count ?? 0)
                    print("\(articleModel.ha_tags ?? "")")
                }
            case false:
                print(response.result.error as Any)
            }
//            if response.error  == nil{
//                print("请求成功")
//                print("\(String(describing: response.result.value))")
//                let json = response.result.value
//                
//                
//            }else{
//                print("请求失败")
//            }
        }
    }

JSON 转 Model的方法

SwfitJson 转model

//Model 的创建
import UIKit
import SwiftyJSON

class ResponseBaseModel: NSObject {
    var message : String?
    var data : Any?
    var code : Int?
    
    override init() {
        super.init()
    }
    
    init(jsonData : JSON) {
        message = jsonData["message"].stringValue
        data = jsonData["data"]
        code = jsonData["code"].intValue
    }
}

//针对返回json的使用
 @objc func getDataSourceAction(){
        Alamofire.request("http://zc.dev.bxd365.com/app/head/head/lists?app_id=zc_ios&uid=179&c_id=255&m_id=1496&api_version=1.2.9&expires=1542091791.764&request_date=2018-11-13%2014:49:51&password=a2d2173f53061554939b716b5f3e8807&ver=1.2.9&page=1&u_id=179&request_sign=c5da3ebf9fba-dba97572ef64&pagenum=20&sign=bfbaef881080cfbacb357ea6d39df7f1", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            
            switch response.result.isSuccess{
           
            case true:
                if let value = response.result.value{
                    print("\(String(describing: response.result.value))")

                    let json = JSON(value)
                    let model = ResponseBaseModel.init(jsonData : json)
                    let dic = JSON(model.data!).dictionaryValue
                    let valueModel = FineListModel.init(jsonData: JSON(dic))
                    let count = valueModel.articleList?.count
                    
                    let articleModel = valueModel.articleList?.first as! FineArticleListModel
                    print(count ?? 0)
                    print("\(articleModel.ha_tags ?? "")")
                }
            case false:
                print(response.result.error as Any)
            }

        }
    }

ObjectMapper 转model

服务器返回的数据
{
    code = 0;
    data =     {
        articleList =         (
                        {
                "ha_id" = 33;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/product_03.jpg";
                "ha_readNum" = 1927;
                "ha_tags" = "\U6d4b\U8bd5\U4e5d\U4e0b";
                "ha_title" = "\U6d4b\U8bd5\U4e5d\U4e0b";
                time = "2018-01-02";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=33";
            },
                        {
                "ha_id" = 32;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/product_03.jpg";
                "ha_readNum" = 573;
                "ha_tags" = "\U6d4b\U8bd5\U516b\U4e0b";
                "ha_title" = "\U6d4b\U8bd5\U516b\U4e0b";
                time = "2018-01-01";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=32";
            },
                        {
                "ha_id" = 43;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//01/1513925499.jpg";
                "ha_readNum" = 720;
                "ha_tags" = 999;
                "ha_title" = "\U7f8e\U4e0a\U5929\U5566\Uff01\U676d\U5dde\U5efa\U4e86\U7b2c\U4e00\U6761\U7a7a\U4e2d\U8dd1\U9053\Uff01\U8dd1\U5708\U7b80\U76f4\U62cd\U5927\U7247";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=43";
            },
                        {
                "ha_id" = 42;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/11.jpg";
                "ha_readNum" = 208;
                "ha_tags" = 66;
                "ha_title" = "\U82f9\U679c\U534a\U5e74\U5185\U8bc4\U7ea7\U4e24\U5ea6\U906d\U4e0b\U8c03 \U5e02\U503c\U4e0d\U52309000\U4ebf\U7f8e\U5143";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=42";
            },
                        {
                "ha_id" = 29;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/product_03.jpg";
                "ha_readNum" = 60;
                "ha_tags" = "\U6d4b\U8bd5\U65e0\U6687,\U6d4b\U8bd5\U516b\U4e0b";
                "ha_title" = "\U6d4b\U8bd5\U4e94\U4e0b";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=29";
            },
                        {
                "ha_id" = 30;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/product_05.jpg";
                "ha_readNum" = 130;
                "ha_tags" = "\U6d4b\U8bd5\U516d\U4e0b,\U6d4b\U8bd5\U516b\U4e0b";
                "ha_title" = "\U6d4b\U8bd5\U516d\U4e0b";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=30";
            },
                        {
                "ha_id" = 28;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/product_03.jpg";
                "ha_readNum" = 56;
                "ha_tags" = "\U6d4b\U8bd5\U56db\U4e0b,\U6d4b\U8bd5\U516b\U4e0b";
                "ha_title" = "\U6d4b\U8bd5\U56db\U4e0b";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=28";
            },
                        {
                "ha_id" = 41;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/8.jpg";
                "ha_readNum" = "1.6\U4e07";
                "ha_tags" = gg;
                "ha_title" = "\U591a\U8089\U690d\U7269\U5143\U7d20\U7684\U65b0\U5a18\U624b\U6367\U82b1 : \U591a\U8089\U690d\U7269\U4ee5\U5176\U53ef\U7231";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=41";
            },
                        {
                "ha_id" = 39;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/20846327_1200x1000_281.jpg";
                "ha_readNum" = 209;
                "ha_tags" = ltt;
                "ha_title" = "\U738b\U601d\U806a\U968f\U793c30\U4e07 \U4e3a\U4ec0\U4e48\U968f\U793c30\U4e07?";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=39";
            },
                        {
                "ha_id" = 38;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/001.jpg";
                "ha_readNum" = 117;
                "ha_tags" = "\U5bf9\U8c01\U90fd";
                "ha_title" = "\U65f6\U95f4\U5c55\U793a";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=38";
            },
                        {
                "ha_id" = 37;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/16120Q04559-5.jpg";
                "ha_readNum" = 38;
                "ha_tags" = "\U70ed\U95e8\U6587\U7ae0";
                "ha_title" = "\U6dfb\U52a0\U4e00\U4e2a\U7406\U5ff5\U7c7b\U7684\U70ed\U95e8\U6587\U7ae0";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=37";
            },
                        {
                "ha_id" = 46;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//oss/Msg/img/1516086690.jpg";
                "ha_readNum" = "1.6\U4e07";
                "ha_tags" = "\U6d4b\U8bd5\U56fe\U7247\U89c6\U9891";
                "ha_title" = "\U6d4b\U8bd5\U56fe\U7247\U89c6\U9891";
                time = "2017-12-20";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=46";
            },
                        {
                "ha_id" = 9;
                "ha_image" = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com//01/003.jpg";
                "ha_readNum" = 84;
                "ha_tags" = "\U7b2c\U4e09\U65b9";
                "ha_title" = "\U6d4b\U8bd5\U56fe\U7247";
                time = "2017-12-13";
                url = "http://zc.dev.bxd365.com/html/head/detail?ha_id=9";
            }
        );
        patentList =         (
                        {
                "hp_describe" = "holiday\U4e13\U9898";
                "hp_id" = 7;
                "hp_name" = "holiday\U4e13\U9898";
                url = "http://zc.dev.bxd365.com/html/head/patent?hp_id=7";
            },
                        {
                "hp_describe" = "\U7b2c\U516d\U4e2a\U4e13\U520a\U8fd8\U663e\U793a\U5417";
                "hp_id" = 6;
                "hp_name" = "\U7b2c\U516d\U4e2a\U4e13\U680f\U8fd8\U663e\U793a\U5417";
                url = "http://zc.dev.bxd365.com/html/head/patent?hp_id=6";
            },
                        {
                "hp_describe" = "\U6211\U662f\U7b2c\U4e94\U4e2a\U4e13\U520a\Uff0c\U4e13\U680f\U5185\U5bb9\U4e0d\U77e5\U9053\U54e6\U554a\U9009\U62e9\U5565\Uff0c\U6709\U8981\U63a8\U8350\U7684\U5417";
                "hp_id" = 5;
                "hp_name" = "\U6211\U662f\U7b2c\U4e94\U4e2a\U4e13\U680f";
                url = "http://zc.dev.bxd365.com/html/head/patent?hp_id=5";
            },
                        {
                "hp_describe" = "\U6211\U662f\U7b2c\U56db\U4e2a\U4e13\U680f";
                "hp_id" = 4;
                "hp_name" = "\U7b2c\U56db\U4e2a\U4e13\U680f";
                url = "http://zc.dev.bxd365.com/html/head/patent?hp_id=4";
            },
                        {
                "hp_describe" = "\U6211\U662f\U7b2c\U4e09\U4e2a\U4e13\U680f\Uff0c\U6d4b\U8bd5\U4e0b\U4e13\U680f\U4fe1\U606f";
                "hp_id" = 3;
                "hp_name" = "\U7b2c\U4e09\U4e2a\U4e13\U680f";
                url = "http://zc.dev.bxd365.com/html/head/patent?hp_id=3";
            }
        );
        recommendList =         (
                        {
                "hr_content" =                 (
                                        {
                        imgurl = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com/oss/Msg/img/002.jpg";
                        picurl = "http://zc.dev.bxd365.com/html/head/subject?hs_id=1";
                    },
                                        {
                        imgurl = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com/oss/Msg/img/16120Q04559-5.jpg";
                        picurl = "http://zc.dev.bxd365.com/html/head/subject?hs_id=2";
                    },
                                        {
                        imgurl = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com/oss/Msg/img/TEST-\U5de6\U8fb9.jpg";
                        picurl = "http://zc.dev.bxd365.com/html/head/subject?hs_id=3";
                    }
                );
                "hr_id" = 10;
                "hr_type" = 3;
            },
                        {
                "hr_content" =                 (
                                        {
                        imgurl = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com/oss/Msg/img/5.jpg";
                        picurl = "https://huaban.com/explore/duoroushoupenghua/";
                    },
                                        {
                        imgurl = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com/oss/Msg/img/6.jpg";
                        picurl = "https://huaban.com/explore/duoroushoupenghua/";
                    },
                                        {
                        imgurl = "http://crowdsource-test.oss-cn-hangzhou.aliyuncs.com/oss/Msg/img/2.jpg";
                        picurl = 1;
                    }
                );
                "hr_id" = 11;
                "hr_type" = 3;
            }
        );
    };
    message = "";
}
//Mark 采用ObjectMapper方式创建的model
import UIKit
import ObjectMapper


struct ObjectResponse: Mappable {
    
    var code : Int?
    var data : DataModel?
    var message: String?
    
    init?(map: Map) {}
    
    mutating func mapping(map: Map) {
        code <- map["code"]
        data <- map["data"]
        message <- map["message"]
    }
}

struct DataModel: Mappable {
    var articleList: [ArticleListModel]?
    var patentList: [PatenListModel]?
    var recommendList: [RecommendModel]?
    
    init?(map: Map) {}
    
    mutating func mapping(map: Map) {
        articleList <- map["articleList"]
        patentList <- map["patentList"]
        recommendList <- map["recommendList"]
    }
    
    
}

struct ArticleListModel: Mappable {
    
    var ha_id: String?
    var ha_image: String?
    var ha_readNum: String?
    var ha_tags: String?
    var ha_title: String?
    var time: String?
    var url: String?

    init?(map: Map) {}
    
    mutating func mapping(map: Map) {
        ha_id <- map["ha_id"]
        ha_image <- map["ha_image"]
        ha_readNum <- map["ha_readNum"]
        ha_tags <- map["ha_tags"]
        ha_title <- map["ha_title"]
        time <- map["time"]
        url <- map["url"]
    }
    
}

struct PatenListModel: Mappable {
    var hp_describe: String?
    var hp_id: String?
    var hp_name: String?
    var url: String?
    
    init?(map: Map) {}
    
    mutating func mapping(map: Map) {
        hp_describe <- map["hp_describe"]
        hp_id <- map["hp_id"]
        hp_name <- map["hp_name"]
        url <- map["url"]

    }
}

struct RecommendModel: Mappable {
    
    var hr_content: [HRContentModel]?
    var hr_id: String?
    var hr_type: String?
    
    init?(map: Map) {}
    
    mutating func mapping(map: Map) {
        hr_content <- map["hr_content"]
        hr_id <- map["hr_id"]
        hr_type <- map["hr_type"]
    }
}

struct HRContentModel: Mappable {
    
    var imgurl: String?
    var picurl: String?
    
    init?(map: Map) {
        
    }
    
    mutating func mapping(map: Map) {
        imgurl <- map["imgurl"]
        picurl <- map["picurl"]
    }
    
}

 

你可能感兴趣的:(swfit 开发学习笔记)