Swift 使用Get请求

不说废话,直接上代码

URLL

import UIKit

class URLL: NSObject {

    func GET(pass:@escaping (Any,Bool)->Void){

        let urlstr = URL.init(string: "http://api.jisuapi.com/illegal/carorg2?appkey=de394933e1a3e2db")

        // 将字符串转换为URL

//        let mURL = URL.init(string: urlstr)

        // 将URL封装为Request对象

        let req = URLRequest(url: urlstr!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0)

        // 网络会话对象,请求网络数据

        URLSession.shared.dataTask(with: req) { (data:Data?, response:URLResponse?, error:Error?) in

            

            // JSON解析

            let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)

        

        

            let jsonDic = jsonData as! NSDictionary

            let status = jsonDic.value(forKey: "status") as! NSString

            let msg = jsonDic.value(forKey: "msg") as! String

            if status.intValue != 0{

                DispatchQueue.main.async {

                    print(msg)

                }

                return

            }

            

            let resultDic = jsonDic.value(forKey: "result") as! NSDictionary

            let dataArr = resultDic.value(forKey: "data") as! NSArray

            

            

            var modelArr:[hqjgj] = []

            for item in dataArr{

                let itemDic = item as! NSDictionary

                let one = hqjgj()

                one.province = (itemDic.value(forKey: "province") as? String)!

                

                

                

                modelArr.append(one)

            }

            pass(modelArr,true)

    }.resume()

    }

    

}

=====================================HqViewController.swift=======================

import UIKit

class HqViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    

    var tableDataDic:[String:[String]]?  // 用于给表格赋值的字典

    var table:UITableView?    // 表格视图

    var tableData:[hqjgj]?

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.table = UITableView(frame: self.view.frame, style: .plain)

        self.table?.dataSource = self;

        self.table?.delegate = self;

        self.view.addSubview(self.table!)

       

    }

    override func viewWillAppear(_ animated: Bool) {

        let urlh = URLL()

        urlh.GET { (data, success) in

            if !success {

                return

            }

            self.tableData = (data as? [hqjgj])!

            DispatchQueue.main.async {

                self.table?.reloadData()

            }

            print(data)

        }

        

        

    }

    

    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        

        

        if let count = tableData?.count {

            return count

        }

        return 0

        

        

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellIdentifier = "cell"

        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)

        if cell == nil {

            cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)

        }

        let one = self.tableData![indexPath.row]

        cell?.textLabel?.text = one.province

        return cell!

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    

}

==================================控件===================================

import UIKit

class ViewController: UIViewController {

    var searchBtn:UIButton// 搜索按钮

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        self.view.backgroundColor = UIColor.white

        

        

        

        searchBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))

        searchBtn?.center = CGPoint(x:200, y: 300)

        searchBtn?.setTitle("点击查询", for: .normal)

        searchBtn?.backgroundColor = UIColor.black

        searchBtn?.setTitleColor(UIColor.white, for: .normal)

        searchBtn?.addTarget(self, action: #selector(btnDidPress(sender:)), for: .touchUpInside)

        self.view.addSubview(searchBtn!)

        

        

    }

    

    @objc func btnDidPress(sender:UIButton) -> Void {

        

        let hqVC = HqViewController()

        self.present(hqVC, animated: true, completion: nil)

        print("进入了第二个控制器")

        

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

=================================hqjgj===================================

class hqjgj: NSObject {

    var province = ""

    

}

猜你喜欢

转载自blog.csdn.net/f9999999995/article/details/80526536