SSAC iOS-네트워크 (3/3)

BINGBING🐨·2021년 10월 25일
0

Swift

목록 보기
8/21

라이브러리 사용해서 JSON 데이터 가져오기

//
//  ViewController.swift
//  SeSSAC-WEEK5-CLASS
//
//  Created by mac on 2021/10/25.
//

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       
        getCurrentWeather()
    }

    func getCurrentWeather(){
        
        let url = "https://api.openweathermap.org/data/2.5/box/city?bbox=12,32,15,37,10&appid=69f4c041bdfafce7925e677a1079844e"
        
        
        AF.request(url, method: .get).validate().responseJSON { response in
            switch response.result {
            case .success(let value): //성공했을 때(let 연관값)
                let json = JSON(value)
                print("JSON: \(json)")
            case .failure(let error): //실패했을 때
                print(error)
            }
        }
    }
}

데이터형식: JSON

String타입에 JSON
Swift로 연결될 수 있게 제이슨시리얼라이제이션 을 해야한다.
근데 요즘은 잘 안쓰는듯???

위에 사용한 라이브러리에서 자동으로 해주고있음


JSON: {
  "cnt" : 15,
  "calctime" : 0.0017665179999999999,
  "list" : [
    {
      "name" : "Birkirkara",
      "rain" : null,
      "weather" : [
        {
          "id" : 801,
          "description" : "few clouds",
          "icon" : "02n",
          "main" : "Clouds"
        }
      ],

GCP/Opertation 에 이어서 포스팅

profile
iOS Developer

0개의 댓글