It's How different
File: jsonRead.swift ------------------------- // // ViewController.swift // jsonRead // // Created by matz on 2014/10/26. // Copyright (c) 2014年 matz. All rights reserved. // import UIKit class ViewController: UIViewController , UITableViewDelegate{ var myObject = [] var titleStr:String? var thumnail:String? var author:String? @IBOutlet var tbView: UITableView! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() self.getData() } func getData(){ /************************************************** // ローカルファイルから取得 var filePath:String = NSBundle.mainBundle().pathForResource("gist", ofType:"json")! let jsonString:String? = String(contentsOfFile: filePath, encoding: NSUTF8StringEncoding, error:nil) if(jsonString != nil){ // JSON 文字列をそのまま NSJSONSerialization に渡せないので、 // NSData に変換する var jsonData = jsonString?.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false) var jsonObjects:AnyObject? = NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers, error: nil) println(jsonObjects) }else{} var data:NSData? = NSData(contentsOfURL: NSURL(string: "https://api.github.com/users/manchan/gists")!) **************************************************/ let URL = NSURL(string: "https://api.github.com/users/manchan/gists") let req = NSURLRequest(URL: URL!) let connection : NSURLConnection? = NSURLConnection(request: req, delegate: self, startImmediately: false) // リクエスト送信 NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue(), completionHandler:response) } // リクエスト終わり次第、実行 func response(res: NSURLResponse!, data: NSData!, error: NSError!){ if let json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: nil) as? NSArray { myObject = json tbView.reloadData() } else { //NSArray is nil } } func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { return myObject.count; } func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! { let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell") cell.textLabel.text = myObject[indexPath.row]["description"] as? String return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }