//声明一个结构体类型的数组
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
}
}