ios - 如何在swift中将字典值存储到字典数组中的数组中

标签 ios arrays dictionary swift tableview

<分区>

我是 iOS 开发的新手,我正在尝试在 tableview 中快速显示数据数据来自网络服务,因为我使用了 NSURLSessionDataTask。数据如下所示:

{ 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"
              }]
   }

我想在表格 View 中显示“category_name”名称。 下面是我尝试过并试图在 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
}
}

最佳答案

希望对您有所帮助。快速代码

    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")!)
            }
        }

    }

关于ios - 如何在swift中将字典值存储到字典数组中的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27053103/

相关文章:

JavaScript 扩展数组并向子原型(prototype)添加方法?

java - 在java中编辑对象二维数组的字段

javascript - 如何访问数组 Javascript 中的对象

excel - 在 VBA 中使用字典会给我的代码带来任何缺陷吗?

ios - 如何在 Swift 中编辑动态 UITableViewCell 中的 View ?

ios - 如何为 block 捕获变量的当前值

objective-c - 如何在 ios 开发中从 mp3 文件中提取元数据

ios - GPUImage:2个分支链的同步

python - 从 python 中的字典获取键列表/键集的最有效方法是什么?

python - 如何用字典计算列表的大小?