It's How different
File: getPrefecture.swift ------------------------- // // ViewController.swift // getPrefecture // // Created by matz on 2014/10/24. // Copyright (c) 2014年 matz. All rights reserved. // import UIKit class ViewController: UIViewController, UIPickerViewDelegate { // 都道府県 var pref: NSArray = [] @IBOutlet var pickView: UIPickerView! @IBOutlet var itemLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() // 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. } @IBAction func load(sender: AnyObject) { getData() } func getData() { let URL = NSURL(string: "http://express.heartrails.com/api/json?method=getPrefectures") 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!){ // 取得したデータをログ表示 // println(data) let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil) as NSDictionary let res: NSDictionary = json.objectForKey("response") as NSDictionary pref = res.objectForKey("prefecture") as NSArray // for var i=0; i Int{ return 1; } func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ return pref.count } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{ return pref[row] as String } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){ var itemSelected = pref[row] as String itemLabel.text = itemSelected } }
@IBOutlet var pickView: UIPickerView! @IBOutlet var itemLabel: UILabel!