JSON Parsing in Swift Language – iOS 8

Here is tutorial about parsing JSON in Swift Language iOS 8

Swift JSON
Swift JSON

Create a Dictionary of all JSON data:
[code language=”obj-c”]
let url=NSURL(string:"http://api.androidhive.info/contacts/")
let allContactsData=NSData(contentsOfURL:url)
var allContacts:Dictionary<String, AnyObject>=NSJSONSerialization.JSONObjectWithData(allContactsData, options: NSJSONReadingOptions.MutableContainers, error: nil) as Dictionary<String, AnyObject>
NSLog("%@", allContacts)
[/code]
Parse the JSON by finding your key:
[code language=”obj-c”]
let contacts : AnyObject? = allContacts["contacts"]
for contacts in allContacts.keys {
println("All = \(contacts)")
let contact : AnyObject? = allContacts[contacts]
let collection = contact! as Array<Dictionary<String, AnyObject>>
for subContact in collection {
let name : AnyObject? = subContact["name"]
let email : AnyObject? = subContact["email"]
names+=name! as String
emails+=email! as String
println("Name: \(name)")
println("Email: \(email)")
}
}
[/code]
Complete code snippet with UITableView:
[code language=”obj-c”]
import UIKit
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource
{
@IBOutlet var tblJson : UITableView = nil
var names: String[] = []
var emails: String[] = []
override func viewDidLoad() {
super.viewDidLoad()
let url=NSURL(string:"http://api.androidhive.info/contacts/")
let allContactsData=NSData(contentsOfURL:url)
var allContacts:Dictionary<String, AnyObject>=NSJSONSerialization.JSONObjectWithData(allContactsData, options: NSJSONReadingOptions.MutableContainers, error: nil) as Dictionary<String, AnyObject>
NSLog("%@", allContacts)
let contacts : AnyObject? = allContacts["contacts"]
for contacts in allContacts.keys {
println("All = \(contacts)")
let contact : AnyObject? = allContacts[contacts]
let collection = contact! as Array<Dictionary<String, AnyObject>>
for subContact in collection {
let name : AnyObject? = subContact["name"]
let email : AnyObject? = subContact["email"]
names+=name! as String
emails+=email! as String
println("Name: \(name)")
println("Email: \(email)")
}
}
println(names)
println(emails)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.names.count;
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cell")
}
cell!.textLabel.text = self.names[indexPath.row]
cell!.detailTextLabel.text = self.emails[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected name : "+names[indexPath.row])
}
}
[/code]
Helping, Learning, Coding 🙂

Create Simple Table (UITableView) in Swift Language iOS8

Here is simple tutorial to create table (UITableView) in Swift Language – iOS 8 – Xcode 6

Swift Table
Swift Table

Attach your UITableView IBOutlet to .swift file
[code language=”obj-c”]
@IBOutlet var tblSwift : UITableView = nil
[/code]
Delegate UITableViewDelegate and UITableViewDataSource to your Controller
[code language=”obj-c”]
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource
{

}
[/code]
Don’t forgot to add the delegate methods otherwise you got error
[code language=”obj-c”]
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource
{
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {

}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

}
}
[/code]
Register your cell
[code language=”obj-c”]
override func viewDidLoad() {
super.viewDidLoad()
self.tblSwift.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view, typically from a nib.
}
[/code]
Following is sample of complete code
[code language=”obj-c”]
import UIKit
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource
{
@IBOutlet var tblSwift : UITableView = nil
var items: String[] = ["This", "is" , "swift" , "language" , ":)"]
override func viewDidLoad() {
super.viewDidLoad()
self.tblSwift.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view, typically from a nib.
}
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! {
var cell:UITableViewCell = self.tblSwift.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected item "+items[indexPath.row]) //or
println("You selected item \(items[indexPath.row])") //or
println("You selected cell #\(indexPath.row)!")
}
}
[/code]
Helping, Learning, Coding 🙂