swift的AFNetworking請求資料和json解析
//
// ViewController.swift
// AFNetworking_swiftDemo
//
// Created by apple on 15/7/27.
// Copyright (c) 2015年紫晶立方科技有限公司. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var tableView:UITableView?
var array = [AnyObject]()
overridefunc viewDidLoad() {
super.viewDidLoad()
self.createTableView()
self.loadData()
}
func createTableView()
{
self.tableView = UITableView(frame: CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-20), style: UITableViewStyle.Plain)
self.tableView?.delegate = self
self.tableView?.dataSource
//self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "swiftCell")
self.tableView?.registerNib(UINib(nibName: "goodDetailCell", bundle: nil), forCellReuseIdentifier: "swiftCell")
self.view.addSubview(self.tableView!)
}
func loadData ()
{
var paramDict: Dictionary
var afManager = AFHTTPRequestOperationManager()
var op = afManager.GET("http://www.3dshijian.com/interface/listgoods", parameters: paramDict, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Voidin
var jsonResult: AnyObject! = NSJSONSerialization.JSONObjectWithData(responseObject as! NSData, options: NSJSONReadingOptions.AllowFragments, error: nil)
//獲取json裡的資料放入陣列
iflet jsonDic = jsonResult as? NSDictionary {
iflet dataDic = jsonDic["data"] as? NSDictionary {
iflet listArray = dataDic["list"] as? NSArray {
for value in listArray {
self.array.append(value)
var model = goodModel()
}
self.tableView?.reloadData()
}
}
}
}) { (operation: AFHTTPRequestOperation!,
error: NSError!) -> Voidin
}
op.responseSerializer = AFHTTPResponseSerializer()
op.start()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identify: String = "swiftCell"
let cell:goodDetailCell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as! goodDetailCell
iflet detailDic = self.array[indexPath.row] as? NSDictionary {
cell.loadData(detailDic)
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
returnself.array.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return100
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println(indexPath.row)
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}