๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/iOS

[iOS] ํ˜„์žฌ ์œ„์น˜ ๋‚ ์”จ & 24์‹œ๊ฐ„ & 7์ผ ๋ฐ์ดํ„ฐ ๋„์šฐ๊ธฐ - 1(Current Location Get weather 24 hours a day, 2 week)

by Fomagran ๐Ÿ’ป 2021. 2. 22.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!!

 

์ €๋ฒˆ ์‹œ๊ฐ„์— ํ˜„์žฌ ์œ„์น˜์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ๋ฐ›์•„์˜ค๊ณ  ํ˜„์žฌ ์œ„์น˜๋ฅผ ์•Œ์•„๋ณด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

ํ˜น์‹œ๋ผ๋„ ๋ชป๋ณด์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ๋กœ ์ด๋™ํ•˜์…”์„œ ๋ณด๊ณ  ์™€์ฃผ์„ธ์š”~

 

์˜ค๋Š˜์€ ํ˜„์žฌ ์œ„์น˜์˜ ๋‚ ์”จ์™€ ์•ž์œผ๋กœ์˜ 24์‹œ๊ฐ„,์•ž์œผ๋กœ์˜ ์ผ์ฃผ์ผ๊ฐ„ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์„œ ํ™”๋ฉด์— ๋„์›Œ๋ณผ๊ฑฐ์—์š”.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Preview

 


Weatherbit.io

 

๋‚ ์”จ ์ •๋ณด๋ฅผ ๋ฐ›๋Š” ์„œ๋น„์Šค๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด weatherbit์—์„œ ์ œ๊ณตํ•˜๋Š” API๋ฅผ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

 

Weatherbit | Weather API - Historical Weather API

Simple. Powerful. Reliable. Getting access to weather data or historical weather data has never been easier. Unparalleled Forecast Accuracy We leverage the power of forecast models, and machine learning to deliver the most accurate weather forecast API on

www.weatherbit.io

์œ„ ์‚ฌ์ดํŠธ๋กœ ์ด๋™ํ•˜์…”์„œ ํšŒ์›๊ฐ€์ž… ํ›„ ๋กœ๊ทธ์ธ ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ™”๋ฉด์ด ๋ณด์ด์‹ค๊ฑฐ์—์š”!

 

์—ฌ๊ธฐ์„œ Key๋ฅผ ์ž˜ ์ €์žฅํ•ด๋‘ก๋‹ˆ๋‹ค.

 

 

๊ทธ๋‹ค์Œ API Docs๋ฅผ ํด๋ฆญํ•˜์‹œ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋œฐ๊ฑด๋ฐ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ API Documentation์„ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

 

 

๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ API Documentation์ด ๋ณด์ด์‹ค๊ฑด๋ฐ ์—ฌ๊ธฐ์„œ ์›ํ•˜์‹œ๋Š” API๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!

 

 

 

๊ทธ๋ฆฌ๊ณ  ๋‚ ์”จ์— ๋”ฐ๋ฅธ ์•„์ด์ฝ˜์ด ํ•„์š”ํ•œ๋ฐ ์•„๋ž˜ ์‚ฌ์ดํŠธ๋กœ ์ ‘์†ํ•˜์…”์„œ

 

 

Weatherbit | Weather API metadata

 

www.weatherbit.io

 

์•„๋ž˜์˜ PNG Archive ํŒŒ์ผ์„ ๋‹ค์šด๋ฐ›์•„์ฃผ์„ธ์š”!

 


Pod

 

์ž ๊ทธ๋Ÿผ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹œ์ž‘ํ•ด๋ณผ๊ฒŒ์š”!

 

Xcode ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์‹œ๊ณ  podfile๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”!

 

๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•ด Alamofire,SwiftyJSON ์ด๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๊ธฐ๋•Œ๋ฌธ์—

 

ํ„ฐ๋ฏธ๋„์—์„œ ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ์ด๋™ ํ›„ pod init์„ ํ•ด์ฃผ์‹œ๊ณ 

 

์—ฌ๋Ÿฌ๋ถ„๋“ค๋„ podfile์— ์ถ”๊ฐ€ํ•˜์‹ ๋’ค pod install ํ•ด์ฃผ์„ธ์š”~

pod 'SwiftyJSON', '~> 4.0'
pod 'Alamofire', '~> 5.2'

Assets

 

์œ„์—์„œ ๋‹ค์šด๋ฐ›์€ icons๋ฅผ

ํ”„๋กœ์ ํŠธ์˜ Assets์— ๋ชจ๋‘ ๋„ฃ์–ด์ฃผ์„ธ์š”!


KeyCenter

 

์œ„์—์„œ ์ €์žฅํ•œ KEY๋ฅผ ์ด๋ ‡๊ฒŒ KeyCenter struct๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

struct KeyCenter {
    static let key = "YOUR_API_KEY"
}

LocationService

 

์ €๋ฒˆ ์‹œ๊ฐ„์— ์–ป์–ด์˜จ ํ˜„์žฌ ์œ„์น˜ ์œ„๋„์™€ ๊ฒฝ๋„๋ฅผ LocationService ์‹ฑ๊ธ€ํ„ด์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ  ์ €์žฅํ•ด์ฃผ์„ธ์š”!

 

(์—ฌ๊ธฐ ์—์„œ ์ €๋ฒˆ ์‹œ๊ฐ„์— ๋ฐฐ์› ๋˜ ์œ„๋„ ๊ฒฝ๋„ ์–ป์–ด์˜ฌ๋•Œ ๋ถ€๋ถ„์„ ์ฐธ์กฐํ•ด์ฃผ์„ธ์š”!) 

class LocationService {
    static var shared = LocationService()
    var longitude:Double!
    var latitude:Double!
}

Layout

 

WeatherView.xib

 

xib ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”!

์œ„ ๋ถ€๋ถ„์€ ํ˜„์žฌ ๋‚ ์”จ๋ฅผ ๋„์šธ ๋ทฐ์ž…๋‹ˆ๋‹ค.

 

๊ฐ cityLabel,dateLabel,tempLabel๋กœ ๊ตฌ์„ฑํ•ด์ฃผ์„ธ์š”.

 

์•„๋ž˜๋Š” hourlyCollectionView๋กœ 24์‹œ๊ฐ„์˜ ๋‚ ์”จ ์ •๋ณด๋ฅผ ์ปฌ๋ ‰์…˜๋ทฐ์ž…๋‹ˆ๋‹ค.

 

์ปฌ๋ ‰์…˜๋ทฐ ์•„๋ž˜ ํ…Œ์ด๋ธ”๋ทฐ๋Š” weeklyTableView๋กœ 2์ฃผ๊ฐ„์˜ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ฌ ํ…Œ์ด๋ธ”๋ทฐ์ž…๋‹ˆ๋‹ค.

WeatherView

 

WeatherView ํŒŒ์ผ์€ ๋งŒ๋“ค์–ด์ฃผ์‹  ํ›„ ์•„์›ƒ๋ ›์„ ๋ชจ๋‘ ์—ฐ๊ฒฐํ•ด์ฃผ์„ธ์š”!

class WeatherView: UIView {

    //MARK:IBOutlets
    
    @IBOutlet weak var cityLabel: UILabel!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var tempLabel: UILabel!
    @IBOutlet weak var weatherInfoLabel: UILabel!
    @IBOutlet weak var weeklyTableView: UITableView!
    @IBOutlet weak var hourlyCollectionView: UICollectionView!

HourlyCollectionViewCell.xib

 

์•„๋ž˜์™€ ๊ฐ™์ด HourlyCollectionViewCell์„ xib๋กœ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ  ์œ„ Label์€ timeLabel๋กœ

 

๊ฐ€์šด๋ฐ ์ด๋ฏธ์ง€๋Š” weatherImage , ์•„๋ž˜๋Š” timeLabel๋กœ ๋ ˆ์ด์•„์›ƒํ•ด์ฃผ์‹œ๊ณ 

HourlyCollectionViewCell

 

HourlyCollectionViewCell์— ๋ชจ๋‘ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

class HourlyCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var tempLabel: UILabel!
    @IBOutlet weak var weatherImage: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!

WeeklyTableViewCell.xib

 

์™ผ์ชฝ Lable์€ ๋‚ ์งœ๋ฅผ ํ‘œ์‹œํ•  weekLabel๋กœ

 

๊ฐ€์šด๋ฐ๋Š” ๋‚ ์”จ ์•„์ด์ฝ˜์„ ํ‘œ์‹œํ•  weatherImage๋กœ

 

์˜ค๋ฅธ์ชฝ ๋ ˆ์ด๋ธ”์€ ์˜จ๋„๋ฅผ ํ‘œ์‹œํ•  tempLabel๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

WeeklyTableViewCell

 

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋‘ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

class WeeklyTableViewCell: UITableViewCell {

    @IBOutlet weak var weekLabel: UILabel!
    @IBOutlet weak var weatherImage: UIImageView!
    @IBOutlet weak var tempLabel: UILabel!

CurrentWeather

 

์ด๋ ‡๊ฒŒ ๋ ˆ์ด์•„์›ƒ์„ ๋๋‚ด์…จ๋‹ค๋ฉด ์ด์ œ ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ์ž‘์—…์„ ํ•ด๋ณผ๊ฒŒ์š”!

 

ํ˜„์žฌ ์œ„์น˜์˜ ๋‚ ์”จ

 

CurrentWeatehr ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

์ œ์ผ ๋จผ์ € Alamofire์™€ SwiftyJSON์„ import ํ•ด์ฃผ์„ธ์š”!

import Alamofire
import SwiftyJSON

 

๊ทธ ๋‹ค์Œ์œผ๋ก  ๋„์‹œ์™€ ๋‚ ์งœ ์˜จ๋„ ๋‚ ์”จ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ฌ ๋ณ€์ˆ˜๋“ค์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 var city:String = ""
 var date:String = ""
 var temp:Double = 0.0
 var weatherInfo:String = ""

์•„๋ž˜์™€ ๊ฐ™์ด ํ˜„์žฌ ๋‚ ์”จ๋ฅผ ๋ฐ›์•„์˜ฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

    func getCurrentWeather(completion:@escaping() -> Void) {
        let lon = LocationService.shared.longitude!
        let lat = LocationService.shared.latitude!
        let path =  "https://api.weatherbit.io/v2.0/current?lat=\(lat)&lon=\( lon)&key=\(KeyCenter.key)&include=minutely"
        
        AF.request(path).responseJSON { (response) in
            let result = response.result
            
            switch result {
            
            case .success(let value as [String:Any]):
                let json = JSON(value)
                let data = json["data"][0]
                self.city = data["city_name"].stringValue
                self.date = currentDateFromUnix(unixDate: data["ts"].double)?.shortDate() ?? ""
                self.temp = data["temp"].double ?? 0.0
                self.weatherInfo = data["weather"]["description"].stringValue
                completion()
                
            case .failure(let error):
                print(error.errorDescription ?? "")
            default:
                fatalError()
            }
        }
    }

๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…๋“œ๋ฆฌ๋ฉด 

 

์œ„์—์„œ lon,lat ๋ณ€์ˆ˜๋Š” LocationService์— ์ €์žฅํ•ด๋‘์—ˆ๋˜ ์œ„๋„์™€ ๊ฒฝ๋„์ž…๋‹ˆ๋‹ค.

let lon = LocationService.shared.longitude!
let lat = LocationService.shared.latitude!

๊ทธ๋ฆฌ๊ณ  path๋ฅผ ์œ„๋„์™€ ๊ฒฝ๋„๋ฅผ ๋„ฃ์–ด์ฃผ์‹œ๊ณ  KeyCenter์— ์ €์žฅํ•ด๋‘์—ˆ๋˜ APIํ‚ค๋ฅผ ์ด์šฉํ•ด์„œ ํ˜„์žฌ ๋‚ ์”จ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด url ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

 let path =  "https://api.weatherbit.io/v2.0/current?lat=\(lat)&lon=\( lon)&key=\(KeyCenter.key)&include=minutely"

 

๊ทธ ๋‹ค์Œ์œผ๋กœ Alamofire ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด path๋ฅผ ํ†ตํ•ด ๋ฐ›์€ JSON ๋ฐ์ดํ„ฐ์˜ ์‘๋‹ต์„ ๋ฐ›์•„์™€์ค๋‹ˆ๋‹ค.

  AF.request(path).responseJSON { (response) in... }

์‘๋‹ต์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ 

  let result = response.result

swiftch case ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•ด ๊ฐ ์‘๋‹ต์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์‘๋‹ต์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋ฐ›์•„์™€์กŒ์„ ๊ฒฝ์šฐ

 

json์œผ๋กœ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ [String:Any] ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ SwiftyJSON ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด

 

JSON(value)๋กœ ๋งŒ๋“ค์–ด์ค€ ๋‹ค์Œ ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์˜ ์ •๋ณด๋ฅผ ๊ฐ ์œ„์น˜์— ๋งž๊ฒŒ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

  switch result {
            case .success(let value as [String:Any]):
                let json = JSON(value)
                let data = json["data"][0]
                self.city = data["city_name"].stringValue
                self.date = currentDateFromUnix(unixDate: data["ts"].double)?.shortDate() ?? ""
                self.temp = data["temp"].double ?? 0.0
                self.weatherInfo = data["weather"]["description"].stringValue
                completion()
                
            case .failure(let error):
                print(error.errorDescription ?? "")
            default:
                fatalError()
            }

JSON ๋ฐ์ดํ„ฐ ์ •๋ณด

 

์•„๋ž˜ ์‚ฌ์ดํŠธ๋กœ ์ด๋™ํ•˜์‹œ๋ฉด json data์— ์–ด๋–ค ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์ด ๋‹ด๊ฒจ์žˆ๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

Weatherbit | Current Weather API Documentation

Current Weather API This API returns current conditions from our network of over 47,000 sub-hourly reporting weather stations. Every API request will return the nearest, and most recent observation. All parameters should be supplied to the Weather API as q

www.weatherbit.io


Helpers & Extensions

 

์œ„์—์„œ currentDateFromUnix์™€ shortDate() ๋ถ€๋ถ„์—์„œ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ๊ฑฐ์—์š”.

 

์ด ๋•Œ ๋‹นํ™ฉํ•˜์ง€ ๋งˆ์‹œ๊ณ  ์ƒˆ๋กœ์šด Swift ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ์‹œ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ™์—ฌ๋„ฃ์–ด์ฃผ์„ธ์š”

 

์‹œ๊ฐ„๊ณผ ๋‚ ์งœ๋ฅผ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.

import UIKit

//Double๋กœ ์˜ค๋Š” ํƒ€์ž„์Šคํƒฌํ”„ ๋ฐ์ดํŠธ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
func currentDateFromUnix(unixDate:Double?) -> Date? {
    if unixDate != nil {
        return Date(timeIntervalSince1970: unixDate!)
    }else{
        return Date()
    }
}

//๋‚ ์”จ ์•„์ด์ฝ˜ ์ด๋ฆ„์— ๋”ฐ๋ผ์„œ ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ™˜
func getWeatherIconFor(_ type:String) -> UIImage? {
    return UIImage(named: type)
}
import Foundation

extension Date {
    func shortDate() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMM d"
        return dateFormatter.string(from: self)
    }
    
    func time() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        return dateFormatter.string(from: self)
    }
}

HourlyWeather

 

24์‹œ๊ฐ„ ๋‚ ์”จ ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ฌ ํด๋ž˜์Šค๋„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

(์ž์„ธํ•œ ์„ค๋ช…์€ ์œ„ CurrentWeather์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.) 

import Alamofire
import SwiftyJSON

class HourlyWeather {
    
    var date:String = ""
    var temp:Double = 0.0
    var weatherIcon:String = ""
    
    init(weatherDictionary:Dictionary<String,Any>) {
        let data = JSON(weatherDictionary)
        self.date = currentDateFromUnix(unixDate: data["ts"].double)?.time() ?? ""
        self.temp = data["temp"].double ?? 0.0
        self.weatherIcon = data["weather"]["icon"].stringValue
    }
    
    class func getHourlyWeather(completion:@escaping([HourlyWeather]) -> Void) {
        
        let lon = LocationService.shared.longitude!
        let lat = LocationService.shared.latitude!
        
        let path = "https://api.weatherbit.io/v2.0/forecast/hourly?lat=\(lat)&lon=\(lon)&key=\(KeyCenter.key)"
        
        AF.request(path).responseJSON { (response) in
            let result = response.result
            var hourlyWeathers = [HourlyWeather]()
            
            switch result {
            
            case .success(let value as [String:Any]):
                    if let data = value["data"] as? [Dictionary<String,AnyObject>] {
                        data.forEach{hourlyWeathers.append(HourlyWeather(weatherDictionary: $0))}
                    }
                completion(hourlyWeathers)
                
            case .failure(let error):
                print(error.errorDescription ?? "")
            default:
                fatalError()
            }
        }
    }
}

WeeklyWeather

 

2์ฃผ๊ฐ„์˜ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ Weeklyweahter ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ถ™์—ฌ๋„ฃ์–ด์ฃผ์„ธ์š”!

 

(์ž์„ธํ•œ ์„ค๋ช…์€ ์œ„ CurrentWeather์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.) 

import Alamofire
import SwiftyJSON

class WeeklyWeather {
    var date:String = ""
    var temp:Double = 0.0
    var weatherIcon:String = ""
    
    
    init(weatherDictionary:Dictionary<String,Any>) {
        let data = JSON(weatherDictionary)
        self.date = String("\(currentDateFromUnix(unixDate: data["ts"].double) ?? Date())".prefix(10))
        self.temp = data["temp"].double ?? 0.0
        self.weatherIcon = data["weather"]["icon"].stringValue
    }
    
    
    class func getWeeklyWeather(completion:@escaping ([WeeklyWeather]) -> Void) {
        
        let lat = LocationService.shared.latitude!
        let lon = LocationService.shared.longitude!
        let path = "https://api.weatherbit.io/v2.0/forecast/daily=7?lat=\(lat)&lon=\(lon)&key=\(KeyCenter.key)"
        
        AF.request(path).responseJSON { (response) in
            let result = response.result
            var weeklyWeathers = [WeeklyWeather]()
            
            switch result {
            case .success(let value as [String:Any]) :
                if let data = value["data"] as? [Dictionary<String,AnyObject>]  {
                    data.forEach {weeklyWeathers.append(WeeklyWeather(weatherDictionary: $0))}
                }
                completion(weeklyWeathers)
            case .failure(let error):
                print(error.errorDescription ?? "")
                
            default :
                fatalError()
            }
        }

    }
}

 


์ ๋‹ค๋ณด๋‹ˆ ๊ธ€์ด ๋„ˆ๋ฌด ๊ธธ์–ด์ ธ์„œ... ใ…œ

 

๋‹ค์Œ ํฌ์ŠคํŒ…์— ๋‚ ์”จ ์ •๋ณด๋ฅผ ๋ฐ›์•„์™€์„œ ๋„์šฐ๋Š”๋ฒ•์„ ์ •๋ฆฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!!

 

๋ชจ๋ฅด์‹œ๋Š” ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์„ธ์š”~~

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€