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<ObjectResponse>().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"]
    }
    
}

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值