svs svs - 3 months ago 28
iOS Question

How to store dictionary values into an array from array of dictionaries in swift

i'm new to iOS development i'm trying to display data in tableview in swift the data is coming from webservice for that i used NSURLSessionDataTask. The data was coming as shown below:

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


I want to display "category_name" names in tableview.
Below is the code what i've tried and trying to display the names in 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
}
}

Answer

Hope this can help you. swift codes

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

    }
Comments