0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

練習のためにSwiftをUdemyで勉強してみた(3日目)

Last updated at Posted at 2020-07-11

3日目にやったこと

-APIを叩いてやってきたJSONが Dictionary だったので、ここから必要な要素を取り出す方法の検討
-色々ググってやりかたを検討した

参考
https://qiita.com/t_punch/items/b5c3de88515f95f80195
https://qiita.com/tenten0213/items/707b1bf2cffb8d3a9700
https://qiita.com/ketancho/items/ad2eb36010fcac620425

現時点での限界

構文なんかを学習せずにケースバイケースでやっていたので、当然ながら何もわからず。
一度しっかりと勉強してから戻ってきたい。

ということで、今日はここまで。
以下は、色々模索する中で作ったViewController.siwftです。
当然動きません。
APIのキーは"hogehoge"にています。

ViewController.siwft
   func CountCovid(){
        let headers = [
            "x-rapidapi-host": "covid-193.p.rapidapi.com",
            "x-rapidapi-key": "hogehoge"
        ]
        let request = NSMutableURLRequest(url: NSURL(string: "https://covid-193.p.rapidapi.com/statistics?country=japan")! as URL,
                                                cachePolicy: .useProtocolCachePolicy,
                                            timeoutInterval: 10.0)
        request.httpMethod = "GET"
        request.allHTTPHeaderFields = headers

        let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            if (error != nil) {
                print(error)
            } else {
                let httpResponse = response as? HTTPURLResponse
                //print(httpResponse)
            }
            do{
                let jsonDataA  = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)as! [String: Any]
                let responses = jsonDataA["response"]
                
                print(typeA)
                
                struct Test: Codable {
                    let cases: String
                    let continent: String
                    let country: String
                    let day: Date
                    let deaths:String
                    let population :Int
                    let tests:String
                    let time:Date
                }
                
                let decoder: JSONDecoder = JSONDecoder()
                do {
                    let Testjson: Test = try decoder.decode(Test.self, from: jsonDataA)
                    print(Testjson)
                } catch {
                    print("error:", error.localizedDescription)
                }
    
             }catch {
                 print(error)
                 }
        })
        dataTask.resume()
    }
}

明日やること

とりあえず、このJSON使うぜーってアプリ制作は一旦停止して、他のアプリ制作を優先します。
都度都度、構文についてもしっかりとドキュメント読みつつ学習していきます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?