Stack OverflowのAPIでswiftの質問を表示
日本語版「Stack Overflow」のAPIを利用してみた! | アシアルブログ
teratailのAPIでswiftの質問を表示 を参考に作成
Usage of /questions [GET] - Stack Exchange API にアクセスして、tagged欄にswiftを入力します。
日本語版のStack Overflowを表示するために、「site=ja.stackoverflow」に変更します。
tag swift
tag react
https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&tagged=react&site=stackoverflow
React
JavaScriptでunixtimeから日時へ変換するやり方のメモ - Qiita
func myload(page: Int , perPage: Int, tag: String) {
let str1:String = "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&tagged=swift&site=ja.stackoverflow"
let str7:String = str1
let url: URL = URL(string: str7)!
let task: URLSessionTask = URLSession.shared.dataTask(with: url, completionHandler: {data, response, error in
do {
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! [String: Any]
print(json)
itemsの下に配列がある
2020-10-12 21:48:51.436840+0900 HelloWorldSwift[33873:1202110] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
["has_more": 1, "quota_remaining": 280, "items": <__NSArrayI 0x600000919900>(
{
"answer_count" = 0;
"content_license" = "CC BY-SA 4.0";
// itemsのデータをself.articlesに入れる(meta,questions,tags)
for (key, value) in articles {
if(key == "items") {
//print("\(key) -> \(value)")
let items = value as! [Any]
タイトルだけ表示するようにして、表示できた。
// セルに表示するタイトルを設定する
let textTitle = cell.viewWithTag(2) as! UILabel
textTitle.text = article["title"]! as? String // items->title
"creation_date" = 1602332072;
SwiftでDateとStringの相互変換 - Qiita
// セルに表示する作成日を設定する
let textDetailText = cell.viewWithTag(3) as! UILabel
let tymeInterval = Double((article["creation_date"]! as? Int)!) // items->creation_date
let createDate = Date(timeIntervalSince1970: tymeInterval)
textDetailText.text = DateUtils.stringFromDate(date: createDate, format: "yyyy-MM-dd HH:mm:ss")
// セルに表示する画像を設定する
let articleOwner = article["owner"] as AnyObject? // items->owner
let profileImageUrl = articleOwner?["profile_image"] // items->owner->profile_image
let profileImage = cell.viewWithTag(1) as! UIImageView
if profileImageUrl != nil { // if profileImageUrl not nil
let myUrl: URL? = URL(string: profileImageUrl as! String)
profileImage.loadImageAsynchronously(url: myUrl, defaultUIImage: nil)
}
// セルに表示する回答数とタグを設定する
let tagsText = cell.viewWithTag(4) as! UILabel
let replayCount = article["answer_count"] as? Int // items->answer_count
let pvCount = article["view_count"] as? Int // items->view_count
var arr = article["tags"] as? [String] // items->tags
let count = arr!.count
let tag1name = "回答数 " + String(replayCount!) + " / PV数 " + String(pvCount!)
+ " / " + (arr?[0])!
tagsText.text = tag1name
if(count > 1) {
arr?.removeFirst()
let tag2name = arr?[0]
tagsText.text = tag1name + "," + tag2name!
if(count > 2) {
arr?.removeFirst()
let tag3name = arr?[0]
tagsText.text = tag1name + "," + tag2name! + "," + tag3name!
if(count > 3) {
arr?.removeFirst()
let tag4name = arr?[0]
tagsText.text = tag1name + "," + tag2name! + "," + tag3name! + "," + tag4name!
if(count > 4) {
arr?.removeFirst()
let tag5name = arr?[0]
tagsText.text = tag1name + "," + tag2name! + "," + tag3name! + "," + tag4name! + "," + tag5name!
}
}
}
}
ページの指定はpageに2を指定
Usage of /questions [GET] - Stack Exchange API
/2.2/questions?page=2&order=desc&sort=activity&tagged=swift&site=stackoverflow
func myload(page: Int , perPage: Int, tag: String) {
let str1:String = "https://api.stackexchange.com/2.2/questions?page="
let str2:String = String(page)
let str3:String = "&order=desc&sort=activity&tagged="
let str4:String = String(tag)
let str5:String = "&site=ja.stackoverflow"
let str7:String = str1 + str2 + str3 + str4 + str5
タイトルで'や"と表示されるので、replacingOccurrncesを追加。replace
Swiftでの文字列置換について|TechRacho(テックラッチョ)〜エンジニアの「?」を「!」に〜|BPS株式会社
// セルに表示するタイトルを設定する
let textTitle = cell.viewWithTag(2) as! UILabel
let tmpTitle = article["title"]! as? String // items->title
let title1 = tmpTitle?.replacingOccurrences(of: "'", with: "'")
let title2 = title1?.replacingOccurrences(of: """, with: "\"")
textTitle.text = title2