{"id":2679,"date":"2018-08-09T12:51:00","date_gmt":"2018-08-09T03:51:00","guid":{"rendered":"http:\/\/www.whoocus.com\/blog\/?p=2679"},"modified":"2018-08-31T09:13:05","modified_gmt":"2018-08-31T00:13:05","slug":"alamofire%e3%81%a7%e5%8f%96%e5%be%97%e3%81%97%e3%81%9f%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92tableview%e3%81%a7%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.blowfish.co.jp\/?p=2679","title":{"rendered":"Alamofire\u3067\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092TableView\u3067\u8868\u793a\u3059\u308b"},"content":{"rendered":"<p>3\u5e74\u4ee5\u4e0a\u3076\u308a\u306b\u3001iOS\u306e\u958b\u767a\u3092\u884c\u3044\u59cb\u3081\u305f\u3002\u4eca\u306e\u6642\u4ee3\u306f<code>swift4<\/code> \u306a\u306e\u306d\u3002<\/p>\n<p>\u25a0 \u958b\u767a\u74b0\u5883<br \/>\nOS : MacOS High Sierra Version 10.13.6<br \/>\nXcode : Version 9.4.1<\/p>\n<p>\u25a0 Server<\/p>\n<p>\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u306f\u3001<code>JSON<\/code>\u30c7\u30fc\u30bf\u3092<code>Ruby on Rails<\/code>\u306e\u3088\u3046\u306b\u3001DB\u30b9\u30ad\u30fc\u30de\u304b\u3089\u7c21\u5358\u306b\u8fd4\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001<code>Elixir<\/code>\u8a00\u8a9e\u3067\u66f8\u304b\u308c\u305f <code>Phoexnix framework<\/code> \u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u305d\u306e\u8fba\u308a\u306f\u3001\u5f8c\u65e5\u306b\u3002<\/p>\n<p>\u25a0 \u53d6\u5f97\u30c7\u30fc\u30bf<br \/>\n\u30ea\u30af\u30a8\u30b9\u30c8\u9001\u4fe1\u5148\u304b\u3089\u8fd4\u3063\u3066\u304f\u308b<code>JSON<\/code>\u30c7\u30fc\u30bf\u306f\u4e0b\u8a18\u306e\u611f\u3058\u3002<\/p>\n<p>[javascript]<br \/>\n{<br \/>\n  &quot;users&quot; : [<br \/>\n    {<br \/>\n      &quot;option&quot; : &quot;\u30b7\u30b9\u30c6\u30e0\u30a8\u30f3\u30b8\u30cb\u30a2&quot;,<br \/>\n      &quot;age&quot; : 46,<br \/>\n      &quot;name&quot; : &quot;\u7530\u5cf6 \u5553\u4e4b&quot;,<br \/>\n      &quot;id&quot; : 1<br \/>\n    },<br \/>\n    {<br \/>\n      &quot;option&quot; : &quot;\u7d4c\u7406&quot;,<br \/>\n      &quot;age&quot; : 49,<br \/>\n      &quot;name&quot; : &quot;\u90fd\u7bc9 \u594f\u5b50&quot;,<br \/>\n      &quot;id&quot; : 2<br \/>\n    },<br \/>\n    {<br \/>\n      &quot;option&quot; : &quot;\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30a8\u30f3\u30b8\u30cb\u30a2&quot;,<br \/>\n      &quot;age&quot; : 45,<br \/>\n      &quot;name&quot; : &quot;\u4e2d\u6751 \u6804\u4eba&quot;,<br \/>\n      &quot;id&quot; : 3<br \/>\n    },<br \/>\n    {<br \/>\n      &quot;option&quot; : &quot;\u7dcf\u52d9&quot;,<br \/>\n      &quot;age&quot; : 38,<br \/>\n      &quot;name&quot; : &quot;\u5343\u8449 \u535a\u5b50&quot;,<br \/>\n      &quot;id&quot; : 4<br \/>\n    }<br \/>\n  ]<br \/>\n}<\/p>\n<p>[\/javascript]<\/p>\n<p>\u25a0 \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u89e3\u8aac<\/p>\n<p><code>ListViewController.swift<\/code>\u306e\u30b3\u30fc\u30c9\u3092\u8aac\u660e\u3059\u308b\u3002<\/p>\n<p>\u307e\u305a\u306f\u3001\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3092<code>import<\/code>\u3059\u308b\u3002\u3053\u3053\u3067\u306f\u3001<code>Alamofire<\/code>\u3068<code>SwiftyJSON<\/code>\u3092\u5229\u7528\u3059\u308b\u3002<\/p>\n<p>[objc]<br \/>\nimport UIKit<br \/>\nimport Alamofire<br \/>\nimport SwiftyJSON<br \/>\n[\/objc]<\/p>\n<p>\u6b21\u306b\u3001<code>Swift4<\/code>\u304b\u3089\u3001<code>Codable<\/code>\u3068\u3044\u3046\u3082\u306e\u3067\u3001\u69cb\u9020\u4f53\u3068JSON\u30c7\u30fc\u30bf\u3092\u7c21\u5358\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3089\u3057\u3044\u306e\u3067\u3001<code>Codable<\/code>\u3092\u5229\u7528\u3057\u3066\u4e0b\u8a18\u306e\u3088\u3046\u306a\u69cb\u9020\u4f53\u3092\u5b9a\u7fa9\u3059\u308b\u3002<br \/>\n\u3053\u308c\u306f\u3001<code>JSON<\/code>\u30c7\u30fc\u30bf\u306e\u30cd\u30b9\u30c8\u69cb\u9020\u3092\u898b\u3066\u308f\u304b\u308b\u3088\u3046\u306b\u3001Users\u69cb\u9020\u4f53\u306e\u4e2d\u306b\u3001User\u69cb\u9020\u4f53\u306e\u30ea\u30b9\u30c8\u304c\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u69cb\u9020\u3068\u306a\u308b\u3002<\/p>\n<p>[objc]<br \/>\nstruct User : Codable{<br \/>\n    let id : Int<br \/>\n    let name: String<br \/>\n    let age : Int<br \/>\n    let option : String<br \/>\n}<br \/>\nstruct Users : Codable{<br \/>\n    let users : [User]<br \/>\n}<br \/>\n[\/objc]<\/p>\n<p><code>ListViewController<\/code>\u30af\u30e9\u30b9\u306f\u3001<code>UITableViewDelegate<\/code>\u3068<code>UITableViewDataSource<\/code>\u3092\u63a1\u7528\u3059\u308b\u3002\u7279\u306b\u554f\u984c\u306f\u306a\u3044\u3002<\/p>\n<p>[objc]<br \/>\nclass ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {<br \/>\n[\/objc]<\/p>\n<p>\u5404\u30e1\u30bd\u30c3\u30c9\u304b\u3089\u53c2\u7167\u53ef\u80fd\u306a\u3088\u3046\u306b\u3001Users\u69cb\u9020\u4f53\u3092\u5b9a\u7fa9\u3059\u308b\u3002\u521d\u671f\u5316\u6642\u306b\u306f\u5024\u304c\u6c7a\u307e\u3089\u306a\u3044\u305f\u3081\u3001nil\u3092\u8a31\u3059\u305f\u3081\u3001\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u3068\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>[objc]<br \/>\n    var users : Users?<br \/>\n[\/objc]<\/p>\n<p><code>viewDidLoad()<\/code>\u306b\u304a\u3044\u3066UI\u306e\u521d\u671f\u5316\u3092\u884c\u3046\u3002<\/p>\n<p>[objc]<br \/>\n    override func viewDidLoad() {<br \/>\n        super.viewDidLoad()<\/p>\n<p>        let tableView : UITableView!<br \/>\n        tableView = UITableView(frame: view.frame, style: .grouped)<br \/>\n        tableView.delegate = self<br \/>\n        tableView.dataSource = self<\/p>\n<p>        view.addSubview(tableView)<\/p>\n<p>[\/objc]<\/p>\n<p><code>Alamofire<\/code>\u306e\u4e3b\u8981\u51e6\u7406\u90e8\u5206\u3002\u307e\u305a\u306f\u3001\u5bfe\u8c61\u30b5\u30fc\u30d0\u3078\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3057\u3066\u3001<code>JSON<\/code>\u3067\u53d7\u3051\u53d6\u308b\u3002\u30ec\u30b9\u30dd\u30f3\u30b9\u304c<code>nil<\/code>\u306e\u5834\u5408\u306f\u3001\u305d\u306e\u307e\u307e<code>return<\/code>\u3002<\/p>\n<p>\u30ec\u30b9\u30dd\u30f3\u30b9\u304c<code>nil<\/code>\u3067\u306a\u3044\u5834\u5408\u306f\u3001<code>JSONDecoder<\/code>\u3067\u3001<code>Users<\/code>\u69cb\u9020\u4f53\u3078\u30de\u30c3\u30d4\u30f3\u30b0\u3059\u308b\u3002<\/p>\n<p><code>tableView.reloadData()<\/code>\u306f\u3001<code>Alamofire<\/code>\u3067\u53d6\u5f97\u3059\u308b\u30c7\u30fc\u30bf\u306f\u975e\u540c\u671f\u306e\u305f\u3081\u3001\u521d\u56de\u306f\u3001<code>tableView<\/code>\u306f\u7a7a\u306e\u305f\u3081\u3001\u8aad\u307f\u8fbc\u307f\u304c\u5b8c\u4e86\u3057\u305f\u6642\u70b9\u3067\u3001<code>tableView<\/code>\u3092\u30ea\u30ed\u30fc\u30c9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<p>[objc]<br \/>\n        \/\/Alamofire<br \/>\n        Alamofire.request(&quot;http:\/\/localhost:4000\/user&quot;)<br \/>\n            .responseJSON{res in<br \/>\n                guard let json = res.data else{<br \/>\n                    return<br \/>\n                }<br \/>\n                self.users = try! JSONDecoder().decode(Users.self, from: json)<\/p>\n<p>            tableView.reloadData()<br \/>\n        }<\/p>\n<p>[\/objc]<\/p>\n<p>\u3053\u3053\u3067\u306f\u3001\u30ea\u30b9\u30c8\u306e\u6570\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308b\u3002\u901a\u4fe1\u304c\u975e\u540c\u671f\u306e\u305f\u3081\u3001\u3053\u306e\u6642\u70b9\u3067\u306f\u69cb\u9020\u4f53\u306fnil\u306a\u306e\u3067\u3001\u521d\u56de\u306f0\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308b\u3002\u30ea\u30ed\u30fc\u30c9\u3055\u308c\u305f\u6642\u306b\u3001Users\u69cb\u9020\u4f53\u306b\u30c7\u30fc\u30bf\u306f\u30bb\u30c3\u30c8\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001<code>users.count<\/code> \u3067\u30c7\u30fc\u30bf\u6570\u304c\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<p>[objc]<br \/>\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {<br \/>\n        if let cnt = self.users?.users.count{<br \/>\n            return cnt<br \/>\n        }<br \/>\n        return 0<br \/>\n    }<br \/>\n[\/objc]<\/p>\n<p>\u3053\u3053\u3067\u306f\u3001\u30bb\u30eb\u306e\u30c7\u30fc\u30bf\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u3002\u3084\u306f\u308a\u3053\u3053\u3082\u3001\u521d\u56de\u306fnil\u306e\u305f\u3081\u3001<code>let if<\/code> \u3067\u3001nil\u30c1\u30a7\u30c3\u30af\u3092\u884c\u3063\u3066\u3044\u308b\u3002<br \/>\n\u30c7\u30fc\u30bf\u304c\u3042\u308c\u3070\u3001cell.textLabel.text \u3092\u540d\u524d\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n<p>[objc]<br \/>\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {<br \/>\n        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: &quot;CELL&quot;)<br \/>\n        if let user = self.users?.users[indexPath.row]{<br \/>\n            cell.textLabel?.text = user.name<br \/>\n        }<br \/>\n        return cell<br \/>\n    }<\/p>\n<p>[\/objc]<\/p>\n<p>\u25a0 \u6700\u5f8c\u306b<\/p>\n<p>\u3053\u3053\u306b\u8a18\u3057\u305f\u30b3\u30fc\u30c9\u306f\u3001\u521d\u56de\u6642\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u304b\u3089\u8868\u793a\u307e\u3067\u306e\u30b5\u30f3\u30d7\u30eb\u7684\u306a\u6700\u4f4e\u9650\u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002\u3082\u3046\u5c11\u3057\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3057\u3066\u3001\u69cb\u9020\u3092\u8a2d\u8a08\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u52d5\u4f5c\u539f\u7406\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u8a18\u8f09\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u9593\u9055\u3044\u7b49\u304c\u3054\u3056\u3044\u307e\u3057\u305f\u3089\u3001\u3054\u6307\u6458\u9858\u3044\u307e\u3059\u3002<\/p>\n<p>\u4e0b\u8a18\u3001\u5168\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<p>ListViewController.swift<\/p>\n<p>[objc]<br \/>\nimport UIKit<br \/>\nimport Alamofire<br \/>\nimport SwiftyJSON<\/p>\n<p>struct User : Codable{<br \/>\n    let id : Int<br \/>\n    let name: String<br \/>\n    let age : Int<br \/>\n    let option : String<br \/>\n}<br \/>\nstruct Users : Codable{<br \/>\n    let users : [User]<br \/>\n}<\/p>\n<p>class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {<\/p>\n<p>    var users : Users?<\/p>\n<p>    override func viewDidLoad() {<br \/>\n        super.viewDidLoad()<\/p>\n<p>        let tableView : UITableView!<br \/>\n        tableView = UITableView(frame: view.frame, style: .grouped)<br \/>\n        tableView.delegate = self<br \/>\n        tableView.dataSource = self<\/p>\n<p>        view.addSubview(tableView)<\/p>\n<p>        \/\/Alamofire<br \/>\n        Alamofire.request(&quot;http:\/\/localhost:4000\/user&quot;)<br \/>\n            .responseJSON{res in<br \/>\n                guard let json = res.data else{<br \/>\n                    return<br \/>\n                }<br \/>\n                self.users = try! JSONDecoder().decode(Users.self, from: json)<\/p>\n<p>                \/\/Debug<br \/>\n                print(&quot;&#8212; User JSON &#8212;&quot;)<br \/>\n                print(JSON(json))<br \/>\n                print(&quot;&#8212; User Info &#8212;&quot;)<br \/>\n                for user in self.users!.users{<br \/>\n                    print(&quot;NAME:&quot; + user.name)<br \/>\n                    print(&quot;AGE:&quot; + String(user.age))<br \/>\n                }<\/p>\n<p>            tableView.reloadData()<br \/>\n        }<br \/>\n    }<\/p>\n<p>    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {<br \/>\n        if let cnt = self.users?.users.count{<br \/>\n            return cnt<br \/>\n        }<br \/>\n        return 0<br \/>\n    }<\/p>\n<p>    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {<br \/>\n        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: &quot;CELL&quot;)<br \/>\n        if let user = self.users?.users[indexPath.row]{<br \/>\n            cell.textLabel?.text = user.name<br \/>\n        }<br \/>\n        return cell<br \/>\n    }<\/p>\n<p>    override func didReceiveMemoryWarning() {<br \/>\n        super.didReceiveMemoryWarning()<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>[\/objc]<\/p>\n<p><em>Lovly Swift!!!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>3\u5e74\u4ee5\u4e0a\u3076\u308a\u306b\u3001iOS\u306e\u958b\u767a\u3092\u884c\u3044\u59cb\u3081\u305f\u3002\u4eca\u306e\u6642\u4ee3\u306fswift4 \u306a\u306e\u306d\u3002 \u25a0 \u958b\u767a\u74b0\u5883 OS : MacOS High Sierra Version 10.13.6 Xcode : Version 9.4.1 \u25a0 Se [&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":[47,50,4],"tags":[],"class_list":["post-2679","post","type-post","status-publish","format-standard","hentry","category-swift","category-xcode","category-tech"],"_links":{"self":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2679","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=2679"}],"version-history":[{"count":5,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2679\/revisions"}],"predecessor-version":[{"id":2689,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=\/wp\/v2\/posts\/2679\/revisions\/2689"}],"wp:attachment":[{"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blowfish.co.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}