{"id":2210,"date":"2015-01-24T16:23:12","date_gmt":"2015-01-24T07:23:12","guid":{"rendered":"http:\/\/www.whoocus.com\/blog\/?p=2210"},"modified":"2015-01-24T16:26:43","modified_gmt":"2015-01-24T07:26:43","slug":"swift%e3%81%a7afnetworking%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6json%e3%83%87%e3%83%bc%e3%82%bf%e5%8f%96%e5%be%97%e3%81%ae%e8%a6%9a%e3%81%88%e6%9b%b8%e3%81%8d%e3%80%82","status":"publish","type":"post","link":"https:\/\/www.blowfish.co.jp\/?p=2210","title":{"rendered":"Swift\u3067AFNetworking\u3092\u5229\u7528\u3057\u3066JSON\u30c7\u30fc\u30bf\u53d6\u5f97\u306e\u899a\u3048\u66f8\u304d\u3002"},"content":{"rendered":"<p>\u7d50\u69cb\u7406\u8ad6\u7684\u306b\u306f\u308f\u304b\u3063\u3066\u3044\u308b\u304c\u3001\u3044\u3056\u30bd\u30fc\u30b9\u3092\u66f8\u3044\u3066\u307f\u308b\u3068\u304b\u306a\u308a\u30cf\u30de\u308b\u3002<br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5236\u7d04\u3092\u719f\u77e5\u3057\u3066\u3044\u306a\u3044\u3068\u3001\u3080\u3057\u308d\u9762\u5012\u3067\u30d0\u30b0\u3092\u8a98\u767a\u3059\u308b\u306e\u3067\u306f\u306a\u3044\u304b\uff1f<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001Qiita\u306b\u8a18\u8f09\u3059\u308b\u524d\u306e\u30c9\u30e9\u30d5\u30c8\u7248\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3002<br \/>\n\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u306a\u3044\u3088\u3046\u306b\u3001\u30a8\u30e9\u30fc\u3092\u907f\u3051\u308b\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3057\u3066\u3044\u308b\u306e\u3067\u7d50\u69cb\u4e0d\u5b89\u3002<\/p>\n<p>MyTableViewController.swift<\/p>\n<p>[scala]<br \/>\nimport UIKit<\/p>\n<p>struct Game {<br \/>\n    init(){}<br \/>\n    var seasonId:String?<br \/>\n    var gameName:String?<br \/>\n    var gameType:String?<br \/>\n    var homeTeamName:String?<br \/>\n    var awayTeamName:String?<br \/>\n    var homeTeamScore: String?<br \/>\n    var awayTeamScore: String?<br \/>\n}<\/p>\n<p>class MyTableViewController: UITableViewController {<\/p>\n<p>    required init(coder aDecoder: NSCoder) {<br \/>\n        self.gameData = Game()<br \/>\n        super.init(coder: aDecoder)<br \/>\n    }<\/p>\n<p>    var gameData:Game<\/p>\n<p>    override func viewDidLoad() {<br \/>\n        super.viewDidLoad()<\/p>\n<p>        \/\/\u30ea\u30af\u30a8\u30b9\u30c8\u30c7\u30fc\u30bf\u53d6\u5f97<br \/>\n        let manager:AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()<br \/>\n        let serializer:AFJSONRequestSerializer = AFJSONRequestSerializer()<br \/>\n        manager.requestSerializer = serializer<br \/>\n        manager.GET(&quot;http:\/\/score-sheet.herokuapp.com\/api\/games\/latest.json&quot;, parameters: nil,<br \/>\n            success: {(operation: AFHTTPRequestOperation!, responsObject: AnyObject!) in<br \/>\n                let responsDict = responsObject as Dictionary&lt;String, AnyObject&gt;<\/p>\n<p>                \/\/\u5024\u53d6\u5f97<br \/>\n                var seasonId:Int = (responsDict[&quot;season_id&quot;] as AnyObject?) as Int<br \/>\n                var gameName:String = (responsDict[&quot;game_name&quot;] as AnyObject?) as String<br \/>\n                var gameType:Dictionary = (responsDict[&quot;game_type&quot;] as AnyObject?) as Dictionary&lt;String, AnyObject&gt;<br \/>\n                var gameTypeName:String = (gameType[&quot;game_type&quot;] as AnyObject?) as String<br \/>\n                var homeTeam:Dictionary = (responsDict[&quot;home_team&quot;] as AnyObject?) as Dictionary&lt;String, AnyObject&gt;<br \/>\n                var awayTeam:Dictionary = (responsDict[&quot;away_team&quot;] as AnyObject?) as Dictionary&lt;String, AnyObject&gt;<br \/>\n                var homeTeamName:String = (homeTeam[&quot;team_name&quot;] as AnyObject?) as String<br \/>\n                var awayTeamName:String = (awayTeam[&quot;team_name&quot;] as AnyObject?) as String<br \/>\n                var homeTeamScore = (responsDict[&quot;home_team_score&quot;] as AnyObject?) as Int<br \/>\n                var awayTeamScore = (responsDict[&quot;away_team_score&quot;] as AnyObject?) as Int<\/p>\n<p>                \/\/\u53d6\u5f97\u30c7\u30fc\u30bf\u8a2d\u5b9a<br \/>\n                self.gameData.seasonId = String(seasonId)<br \/>\n                self.gameData.gameName = gameName<br \/>\n                self.gameData.gameType = gameTypeName<br \/>\n                self.gameData.homeTeamName = homeTeamName<br \/>\n                self.gameData.awayTeamName = awayTeamName<br \/>\n                self.gameData.homeTeamScore = String(homeTeamScore)<br \/>\n                self.gameData.awayTeamScore = String(awayTeamScore)<\/p>\n<p>                \/\/\u30c7\u30fc\u30bf\u30ea\u30ed\u30fc\u30c9<br \/>\n                self.tableView.reloadData()<\/p>\n<p>                \/\/\u30c6\u30fc\u30d6\u30eb\u30d3\u30e5\u30fc\u4f5c\u6210<br \/>\n                let tableView = MyTableView(frame: self.view.frame, style: UITableViewStyle.Plain)<br \/>\n                tableView.delegate = self<br \/>\n                tableView.dataSource = self<br \/>\n                self.view.addSubview(tableView)<\/p>\n<p>                \/\/\u30bb\u30eb\u8a2d\u5b9a<br \/>\n                let xib = UINib(nibName: &quot;MyTableViewCell&quot;, bundle: nil)<br \/>\n                tableView.registerNib(xib, forCellReuseIdentifier: &quot;BfCell&quot;)<br \/>\n            },<br \/>\n            failure: {(operation: AFHTTPRequestOperation!, error: NSError!) in<br \/>\n                println(&quot;Error!!&quot;)<br \/>\n            }<br \/>\n        )<br \/>\n    }<\/p>\n<p>    override func didReceiveMemoryWarning() {<br \/>\n        super.didReceiveMemoryWarning()<br \/>\n        \/\/ Dispose of any resources that can be recreated.<br \/>\n    }<\/p>\n<p>    \/\/ MARK: &#8211; Table view data source<\/p>\n<p>    override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {<br \/>\n        \/\/ #warning Potentially incomplete method implementation.<br \/>\n        \/\/ Return the number of sections.<br \/>\n        return 1<br \/>\n    }<\/p>\n<p>    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {<br \/>\n        \/\/ #warning Incomplete method implementation.<br \/>\n        \/\/ Return the number of rows in the section.<br \/>\n        return 5<br \/>\n    }<\/p>\n<p>    \/\/\u30bb\u30eb\u30c7\u30fc\u30bf\u8a2d\u5b9a<br \/>\n    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {<br \/>\n        let cell = tableView.dequeueReusableCellWithIdentifier(&quot;BfCell&quot;, forIndexPath: indexPath) as MyTableViewCell<\/p>\n<p>        cell.seasonId?.text = self.gameData.seasonId<br \/>\n        cell.gameName?.text = self.gameData.gameName<br \/>\n        cell.gameType?.text = self.gameData.gameType<br \/>\n        cell.homeTeamName?.text = self.gameData.homeTeamName<br \/>\n        cell.awayTeamName?.text = self.gameData.awayTeamName<br \/>\n        cell.homeTeamScore?.text = self.gameData.homeTeamScore<br \/>\n        cell.awayTeamScore?.text = self.gameData.awayTeamScore<\/p>\n<p>        return cell<br \/>\n    }<\/p>\n<p>    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {<br \/>\n        \/\/let cell = tableView.dequeueReusableCellWithIdentifier(&quot;BfCell&quot;) as UITableViewCell<br \/>\n        \/\/return cell.bounds.height<br \/>\n        return 100<br \/>\n    }<br \/>\n    \/*<br \/>\n    \/\/ Override to support conditional editing of the table view.<br \/>\n    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -&gt; Bool {<br \/>\n        \/\/ Return NO if you do not want the specified item to be editable.<br \/>\n        return true<br \/>\n    }<br \/>\n    *\/<\/p>\n<p>    \/*<br \/>\n    \/\/ Override to support editing the table view.<br \/>\n    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {<br \/>\n        if editingStyle == .Delete {<br \/>\n            \/\/ Delete the row from the data source<br \/>\n            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)<br \/>\n        } else if editingStyle == .Insert {<br \/>\n            \/\/ Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view<br \/>\n        }<br \/>\n    }<br \/>\n    *\/<\/p>\n<p>    \/*<br \/>\n    \/\/ Override to support rearranging the table view.<br \/>\n    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {<\/p>\n<p>    }<br \/>\n    *\/<\/p>\n<p>    \/*<br \/>\n    \/\/ Override to support conditional rearranging of the table view.<br \/>\n    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -&gt; Bool {<br \/>\n        \/\/ Return NO if you do not want the item to be re-orderable.<br \/>\n        return true<br \/>\n    }<br \/>\n    *\/<\/p>\n<p>    \/*<br \/>\n    \/\/ MARK: &#8211; Navigation<\/p>\n<p>    \/\/ In a storyboard-based application, you will often want to do a little preparation before navigation<br \/>\n    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {<br \/>\n        \/\/ Get the new view controller using [segue destinationViewController].<br \/>\n        \/\/ Pass the selected object to the new view controller.<br \/>\n    }<br \/>\n    *\/<\/p>\n<p>}<\/p>\n<p>[\/scala]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7d50\u69cb\u7406\u8ad6\u7684\u306b\u306f\u308f\u304b\u3063\u3066\u3044\u308b\u304c\u3001\u3044\u3056\u30bd\u30fc\u30b9\u3092\u66f8\u3044\u3066\u307f\u308b\u3068\u304b\u306a\u308a\u30cf\u30de\u308b\u3002 \u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5236\u7d04\u3092\u719f\u77e5\u3057\u3066\u3044\u306a\u3044\u3068\u3001\u3080\u3057\u308d\u9762\u5012\u3067\u30d0\u30b0\u3092\u8a98\u767a\u3059\u308b\u306e\u3067\u306f\u306a\u3044\u304b\uff1f \u3068\u308a\u3042\u3048\u305a\u3001Qiita\u306b\u8a18\u8f09\u3059\u308b\u524d\u306e\u30c9\u30e9\u30d5\u30c8\u7248\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3002 \u30a8\u30e9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"saved_in_kubio":false,"footnotes":""},"categories":[49,47,4],"tags":[],"class_list":["post-2210","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","category-tech"],"_links":{"self":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2210"}],"version-history":[{"count":3,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2210\/revisions"}],"predecessor-version":[{"id":2213,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2210\/revisions\/2213"}],"wp:attachment":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}