swfit 网络请求对应json转模型!

<pre name="code" class="plain">
import UIKitimport Alamofireclass FindTableViewController: UITableViewController {
 
//声明一个结构体类型的数组
    var array:[Re] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "发现"
        setData()
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }

    func setData(){
        Alamofire.request(.GET, url).responseJSON { (response) in
            guard let dict = response.result.value as? NSDictionary else { return }
            let item = dict.valueForKey("data") as! NSDictionary
            let items = item.valueForKey("res") as! NSArray
            for dic in items{
                let x = dic as! NSDictionary
//Re是对应的结构体类 字典直接转成结构体类 
               let re = Re(fromDictionary: x)
                self.array.append(re)
            }
            self.tableView.reloadData()
            
        }
       
       
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
//模型类
</pre><pre name="code" class="plain">
</pre><p><pre name="code" class="plain">import Foundation

struct Re{

	var aid : String!
	var arinfo : String!
	var artype : String!
	var click : String!
	var coverpic : String!
	var fromer : String!
	var heightWidth : String!
	var senddate : String!
	var senddateOld : String!
	var showTime : String!
	var smallPic : String!
	var sound1 : String!
	var title : String!
	var up : String!
	var vide1 : String!
	var video1Long : String!
	var viewsort : String!


	/**
	 * 用字典来初始化一个实例并设置各个属性值
	 */
	init(fromDictionary dictionary: NSDictionary){
		aid = dictionary["aid"] as? String
		arinfo = dictionary["arinfo"] as? String
		artype = dictionary["artype"] as? String
		click = dictionary["click"] as? String
		coverpic = dictionary["coverpic"] as? String
		fromer = dictionary["fromer"] as? String
		heightWidth = dictionary["height_width"] as? String
		senddate = dictionary["senddate"] as? String
		senddateOld = dictionary["senddate_old"] as? String
		showTime = dictionary["show_time"] as? String
		smallPic = dictionary["small_pic"] as? String
		sound1 = dictionary["sound1"] as? String
		title = dictionary["title"] as? String
		up = dictionary["up"] as? String
		vide1 = dictionary["vide1"] as? String
		video1Long = dictionary["video1_long"] as? String
		viewsort = dictionary["viewsort"] as? String
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值