NEWS APIを利用して、下記のようなJSONデータを取得しています。
JSON
1{ 2 "status" : "ok", 3 "articles" : [ 4 { 5 "source" : { 6 "id" : null, 7 "name" : "Gigazine.net" 8 }, 9 "author" : null, 10 "urlToImage" : "https://i.gzn.jp/img/2019/01/11/google-supports-dns-over-tls/00_m.jpg", 11 "title" : "Googleの無料DNSサービス「Google Public DNS」がTLSセキュリティに対応 - GIGAZINE", 12 "content" : null, 13 "publishedAt" : "2019-01-11T04:00:00Z", 14 "description" : "Googleが2009年12月3日に提供を開始した無料パブリックDNSサービス「Google Public DNS」は、サービス開始後から無料の公共インターネットサービスの充実などによって大きな成長を遂げました。そんなGoogle Public DNSが、TLSを用いた接続をサポートしました。", 15 "url" : "http://gigazine.net/news/20190111-google-supports-dns-over-tls/" 16 }, 17 { 18 "source" : { 19 "id" : null, 20 "name" : "Gizmodo.jp" 21 }, 22 "author" : "塚本直樹", 23 "urlToImage" : "https://assets.media-platform.com/gizmodo/dist/images/2019/01/10/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%882019-01-104.23.33-w960.jpg", 24 "title" : "Alcatelの新スマホは120ドルなのに使える! #CES2019 - ギズモード・ジャパン", 25 "content" : "1 Alcatel CES 2019GizmodoSam Rutherford 100Alcatel1X Alcatel 1X2019 21,440×720 vs 960×4805.35.5 Image: Gizmodo US 1X 1300200 1X 1.5GHzMediatek MT6739WWRAM1GB2GB OSAndroid GoAndroid 8.1 OreoAndroid 9 Pie5 microUSB 3,000mAh LGG7 Image: Gizmodo US UI Image: Gizm… [+47 chars]", 26 "publishedAt" : "2019-01-11T03:30:00Z", 27 "description" : "日本ではあまり馴染みがないですが、Alcatel(アルカテル)というフランスのスマホメーカーが、CES 2019に新しいスマホを出展しています。実機を触った米Gizmodo、Sam Rutherfordのハンズオンをどうぞ。", 28 "url" : "https://www.gizmodo.jp/2019/01/alcatel-1x-2019-hands-on.html" 29 }, 30 { 31 "source" : { 32 "id" : null, 33 "name" : "Gamespark.jp" 34 }, 35 "author" : "Arkblade", 36 "urlToImage" : "https://www.gamespark.jp/imgs/ogp_f/279563.jpg", 37 "title" : "最大32人対戦も!Steam『ボンバーパーティー!トーナメント』期間限定無料配布―『ボンバーマン』風e-Sports志向ゲーム - Game*Spark", 38 "content" : null, 39 "publishedAt" : "2019-01-11T03:00:03Z", 40 "description" : "Victory Lap Gamesは、Steamにて早期アクセス実施中のパズルアクション『ボンバーパーティー!トーナメント(Blast Zone! Tournament)』の正式版配信日を決定したと共に、Steam版の期間限定での無料配布を開始しました。", 41 "url" : "https://www.gamespark.jp/article/2019/01/11/86558.html" 42 }, 43 { 44 "source" : { 45 "id" : null, 46 "name" : "Impress.co.jp" 47 }, 48 "author" : null, 49 "urlToImage" : "https://pc.watch.impress.co.jp/img/pcw/list/1164/198/DSC01817.jpg", 50 "title" : "LG、WQHDパネル2枚分の49型32:9ウルトラワイド液晶 - PC Watch", 51 "content" : "LG 8()CES 20194949WL95C49WL900G2 4932:95,120×1,440WQHD(2,560×1,440)2 HDR10USB Type-CUSB PD49WL95C85WPDPC HDMI×2DisplayPort3PbP", 52 "publishedAt" : "2019-01-11T02:59:06Z", 53 "description" : "LGは、8日(米国時間)より開幕した「CES 2019」にて、49型ウルトラワイド液晶ディスプレイ「49WL95C」、「49WL900G」の2製品を展示していた。", 54 "url" : "https://pc.watch.impress.co.jp/docs/news/event/1164198.html" 55 }, 56 { 57 "source" : { 58 "id" : null, 59 "name" : "Impress.co.jp" 60 }, 61 "author" : null, 62 "urlToImage" : "https://dc.watch.impress.co.jp/img/dcw/list/1164/165/01.jpg", 63 "title" : "OM-D Special Forum 2019 - デジカメ Watch", 64 "content" : "OM-DOM-D Special Forum 201922 507 Pro galleryHands-on Web1311024 January 2019Hands-on", 65 "publishedAt" : "2019-01-11T02:18:32Z", 66 "description" : "オリンパス株式会社は、OM-Dの「機動力」をテーマとした写真家の作品展示&トークライブイベント「OM-D Special Forum 2019」を2月2日から大阪、東京、名古屋で順次開催する。入場料は無料。", 67 "url" : "https://dc.watch.impress.co.jp/docs/news/event/1164165.html" 68 } 69 ], 70 "totalResults" : 70 71}
上記のデータを下記のCodableプロトコルを適用したStructの形にdecodeしたいですが、decodeする前の処理の方法がわからず行き詰まってしまいました。
Swift
1import Foundation 2 3struct Article: Codable { 4 5 let title : String 6 let url : String 7 8} 9 10struct ArticlesListResult: Codable { 11 12 let status : String 13 let articles : [Article] 14 let totalResults : Int 15 16}
下記がdecode部分のメソッドです。
Swift
1func getNewsData() { 2 Alamofire.request(urlForApi, method: .get) 3 .responseJSON { response in 4 if response.result.isSuccess { 5 let data = response.result.value! 6 let articleList = try! JSONDecoder().decode(ArticlesListResult.self, from: data as! Data) 7 } else { 8 print("Error: (String(describing: response.result.error))") 9 } 10 } 11}
上記コードですと、
Could not cast value of type '__NSDictionaryI' (0x10326ca98) to 'NSData' (0x10326b580).
というエラーが出てしまいます。
この場合、response.result.valueをData型に変換する必要があるのかなと思うのですが、その方法がわかりません。
どなたかご教授いただけますと幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/11 06:07
2019/01/11 06:15
2019/01/11 06:50