Ios 如何将字典值从swift中的字典数组存储到数组中
我是iOS开发新手,我正在尝试在swift的tableview中显示数据。数据来自我使用NSURLSessionDataTask的webservice。数据如下所示:Ios 如何将字典值从swift中的字典数组存储到数组中,ios,arrays,dictionary,swift,tableview,Ios,Arrays,Dictionary,Swift,Tableview,我是iOS开发新手,我正在尝试在swift的tableview中显示数据。数据来自我使用NSURLSessionDataTask的webservice。数据如下所示: { categorys: [ { category_id: "all", category_name: "ALL", category_desc: "All Categories"
{ categorys: [
{
category_id: "all",
category_name: "ALL",
category_desc: "All Categories"
},
{
category_id: "1",
category_name: "Art",
category_desc: "Artffsdfsdfds "
},
{
category_id: "4",
category_name: "Education",
category_desc: "Education for children and adults"
}]
}
我想在tableview中显示“类别名称”。
下面是我尝试并试图在tableview中显示名称的代码
import UIKit
class ViewController: UIViewController, UITableViewDelegate
{
@IBOutlet var dataTableView: UITableView!
var items: NSArray = []
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//table
self.dataTableView!.delegate = self
self.dataTableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(self.dataTableView)
getData({data, error -> Void in
if (data != nil)
{
self.items = NSMutableArray(array: data)
self.dataTableView!.reloadData()
}
else
{
println("api.getData failed")
println(error)
}
})
}
func getData(completionHandler: ((NSArray!, NSError!) -> Void)!) -> Void
{
let url = NSURL(string: "http://sample.com/srd/games/category")!
var request: NSURLRequest = NSURLRequest(URL:url)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
if (error != nil)
{
return completionHandler(nil, error)
}
var error: NSError?
let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
let names = json["categorys"] as NSArray
println(names)
})
task.resume()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
cell.textLabel.text = self.items[indexPath.row]["categorys"] as? NSString
return cell
}
}
希望这能帮助你。swift代码
var dic_categorys : NSDictionary! // original Dictionary
//
var array_item : NSArray! = dic_categorys.valueForKey("categorys") as NSArray
if let array = array_item {
var array_list : NSMutableArray! = NSMutableArray(array:array)
var array_list_category_name : NSMutableArray = NSMutableArray()
//
for item in array_list {
var dic_item : NSDictionary! = item as NSDictionary
if let dic = dic_item {
array_list_category_name.addObject(dic.valueForKey("category_name")!)
}
}
}
谢谢你的回答JNY,在上面我可以得到字典数组,意思是“分类”数组。我无法检索“类别名称”。不幸的是,你的代码对我也不起作用。当我放入您的代码并运行时,它崩溃了。打印您的dic_类别!哪一行代码崩溃了?在这一行,我得到了崩溃“var array_item:NSArray!=dic_categorys.valueForKey(“categorys”)作为NSArray”。该错误是致命错误:在展开可选值打印数组描述时意外发现nil_项:打印数组描述:(NSArray)array=0x000000010f489c13{ObjectiveC.NSObject={}}@SVS请,您需要设置“dic_类别”的值!!!我只是给它下个定义。谢谢@JNYJ它对我有用,谢谢你的回答。