現在、以下のサイトの通りにクイズアプリを作成しようとしています。
そこで、jsonファイルの読み込みをうまくすることができず、質問させていただきます。
Swift
1import UIKit 2import SwiftyJSON 3 4class ListViewController: UIViewController, UINavigationBarDelegate,UITableViewDelegate, UITableViewDataSource { 5 6 var items:[Int] = [] 7 var titles = [Int:String]() 8 9 //ステータスバーを非表示に 10 override func prefersStatusBarHidden() -> Bool { 11 return true 12 } 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 // Do any additional setup after loading the view, typically from a nib. 17 18 //値を受け取る 19 let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 20 let genre = appDelegate.genre 21 22 // jsonファイル読み込み 23 let path = NSBundle.mainBundle().pathForResource("quiz", ofType: "json") 24 let jsondata = NSData(contentsOfFile: path!) 25 26 //swifty 27 let json = JSON(data:jsondata!) 28 29 for ( i:String , v:JSON) in json[genre!] { 30 self.items.append(v["id"].int!) 31 self.titles[v["id"].int!] = v["title"].string! 32 } 33 34 let displayWidth: CGFloat = self.view.frame.width 35 let displayHeight: CGFloat = self.view.frame.height 36 37 // TableViewの生成する(navigation barの高さ分ずらして表示). 38 let myTableView: UITableView = UITableView(frame: CGRect(x: 0, y: 44, width: displayWidth, height: displayHeight - 44)) 39 40 myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") 41 42 myTableView.dataSource = self 43 myTableView.delegate = self 44 45 self.view.addSubview(myTableView) 46 } 47 48 override func viewDidAppear(animated: Bool) { 49 50 let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44)) 51 navigationBar.backgroundColor = UIColor.whiteColor() 52 navigationBar.delegate = self; 53 54 let navigationItem = UINavigationItem() 55 56 let leftButton = UIBarButtonItem(title: "戻る", style: UIBarButtonItemStyle.Plain, target: self, action: "back") 57 navigationItem.leftBarButtonItem = leftButton 58 navigationBar.items = [navigationItem] 59 60 self.view.addSubview(navigationBar) 61 } 62 63 // ジャンル選択画面に戻る 64 func back(){ 65 let genreViewController: UIViewController = ViewController() 66 genreViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve 67 self.presentViewController(genreViewController, animated: true, completion: nil) 68 } 69 70 //Cellが選択された際に呼び出される. 71 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 72 let id = items[indexPath.row] 73 print("押された問題のidは: \(id)") 74 } 75 76 //Cellの総数(jsonから取得した当該ジャンルの問題分だけ) 77 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 78 return titles.count 79 } 80 81 //Cellに値を設定する. 82 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 83 let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyCell") 84 85 let id = items[indexPath.row] 86 87 //問題のタイトル 88 cell.textLabel!.text = titles[id] 89 cell.textLabel?.font = UIFont.systemFontOfSize(14) 90 91 //矢印アイコン 92 cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 93 94 //当該問題に関する情報を表示 95 cell.detailTextLabel?.textColor = UIColor.grayColor() 96 cell.detailTextLabel?.text = "挑戦回数:3回 正答率:80% 前回:2015.04.08" 97 cell.detailTextLabel?.font = UIFont.systemFontOfSize(10) 98 99 return cell 100 } 101 102 override func viewWillDisappear(animated: Bool) { 103 super.viewWillDisappear(animated) 104 105 //別画面へ遷移する前に呼ばれる処理 106 print("移動します") 107 } 108 109 override func didReceiveMemoryWarning() { 110 super.didReceiveMemoryWarning() 111 // Dispose of any resources that can be recreated. 112 } 113} 114
以上のコードの17-18行目の部分でエラーが出てしまいます。
Swift
1for ( i:String , v:JSON) in json[genre!] { 2 self.items.append(v["id"].int!) 3 self.titles[v["id"].int!] = v["title"].string! 4 }
エラー内容を見ると、16行目の部分でvを正しく定義できていないようなのですが、解決方法を教えていただければと思います。
あなたの回答
tips
プレビュー