質問編集履歴
1
エンコードしたコードをのせました
title
CHANGED
File without changes
|
body
CHANGED
@@ -183,6 +183,76 @@
|
|
183
183
|
|
184
184
|

|
185
185
|
|
186
|
+
###urlエンコードしたコード
|
187
|
+
|
188
|
+
```swift
|
189
|
+
import UIKit
|
190
|
+
import Accounts
|
191
|
+
import SwiftyJSON
|
192
|
+
import Alamofire
|
193
|
+
|
194
|
+
class ViewController: UIViewController, UITableViewDataSource {
|
195
|
+
|
196
|
+
var articles: [[String: String?]] = []
|
197
|
+
let table = UITableView()
|
198
|
+
|
199
|
+
override func viewDidLoad() {
|
200
|
+
super.viewDidLoad()
|
201
|
+
title = "記事一覧"
|
202
|
+
|
203
|
+
table.frame = view.frame
|
204
|
+
view.addSubview(table)
|
205
|
+
table.dataSource = self
|
206
|
+
|
207
|
+
getArticles()
|
208
|
+
}
|
209
|
+
|
210
|
+
func getArticles() {
|
211
|
+
let appid = "AIzaSyCAIznRo4MUdgBtijMnd2De9YEUqMdmwaY"
|
212
|
+
let entryUrl: String = "https://www.googleapis.com/youtube/v3/search?key=AIzaSyCAIznRo4MUdgBtijMnd2De9YEUqMdmwaY&q="
|
213
|
+
|
214
|
+
let inputText = entryUrl + "へぼてっく" + "&part=snippet&maxResults=10&order=viewCount"
|
215
|
+
|
216
|
+
guard let escapedInputText = inputText.addingPercentEncoding(
|
217
|
+
withAllowedCharacters: CharacterSet.urlQueryAllowed
|
218
|
+
) else {
|
219
|
+
return
|
220
|
+
}
|
221
|
+
|
222
|
+
Alamofire.request(escapedInputText)
|
223
|
+
.responseJSON { response in
|
224
|
+
guard let object = response.result.value else {
|
225
|
+
return
|
226
|
+
}
|
227
|
+
|
228
|
+
let json = JSON(object)
|
229
|
+
print(json)
|
230
|
+
json.forEach { (_, json) in
|
231
|
+
let article: [String: String?] = [
|
232
|
+
"title": json["items"]["snippet"]["title"].string,
|
233
|
+
"userId": json["items"]["id"]["videoId"].string,
|
234
|
+
|
235
|
+
]
|
236
|
+
self.articles.append(article)
|
237
|
+
}
|
238
|
+
self.table.reloadData()
|
239
|
+
}
|
240
|
+
}
|
241
|
+
|
242
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
243
|
+
return articles.count
|
244
|
+
}
|
245
|
+
|
246
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
247
|
+
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
|
248
|
+
let article = articles[indexPath.row]
|
249
|
+
cell.textLabel?.text = article["title"]!
|
250
|
+
cell.detailTextLabel?.text = article["userId"]!
|
251
|
+
return cell
|
252
|
+
}
|
253
|
+
}
|
254
|
+
```
|
255
|
+
|
186
256
|
### 補足情報(FW/ツールのバージョンなど)
|
187
257
|
|
188
258
|
swift4.1
|