250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- HTML
- button
- css3
- keyframes
- MAC
- iOS 개발자
- hover
- image
- Animation
- 애니메이션
- CSS
- 자바스크립트
- 프론트엔드
- iPhone
- ipad
- IOS
- xcode
- javascript
- SWIFT
- php
- 백엔드
- 비전공 개발자
- 풀스택
- 개발자
- front-end
- html5
- jQuery
- 비전공자
- react
- effect
Archives
- Today
- Total
비전공자 개발일기
Weather Information 본문
728x90
SMALL
import Foundation
struct ErrorMsg: Codable {
let message: String
}
import Foundation
struct WeatherInformation: Codable {
let weather: [Weather]
let temp: Temp
let name: String
enum CodingKeys: String, CodingKey {
case weather
case temp = "main"
case name
}
}
struct Weather: Codable {
let id: Int
let main: String
let description: String
let icon: String
}
struct Temp: Codable {
let temp: Double
let feelsLike: Double
let minTemp: Double
let maxTemp: Double
enum CodingKeys: String, CodingKey {
case temp
case feelsLike = "feels_like"
case minTemp = "temp_min"
case maxTemp = "temp_max"
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var cityNameTextField: UITextField!
@IBOutlet weak var cityNameLabel: UILabel!
@IBOutlet weak var weatherDescriptionLabel: UILabel!
@IBOutlet weak var tempLabel: UILabel!
@IBOutlet weak var maxTempLabel: UILabel!
@IBOutlet weak var minTempLabel: UILabel!
@IBOutlet weak var weatherStackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func searchWeatherBTN(_ sender: UIButton) {
if let cityName = cityNameTextField.text {
getCurrentWeahter(cityName: cityName)
view.endEditing(true)
}
}
func configureView(weatherInformation: WeatherInformation) {
cityNameLabel.text = weatherInformation.name
if let weahter = weatherInformation.weather.first {
weatherDescriptionLabel.text = weahter.description
}
tempLabel.text = "\(Int(weatherInformation.temp.temp - 273.15))°C"
maxTempLabel.text = "MAX: \(Int(weatherInformation.temp.maxTemp - 273.15))°C"
minTempLabel.text = "MIN: \(Int(weatherInformation.temp.minTemp - 273.15))°C"
}
func showAlert(msg: String) {
let alert = UIAlertController(title: "Error", message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
func getCurrentWeahter(cityName: String) {
guard let url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}") else { return }
let session = URLSession(configuration: .default)
session.dataTask(with: url) { [weak self] data, response, error in
let successRange = (200..<300)
guard let data = data, error == nil else { return }
let decoder = JSONDecoder()
if let response = response as? HTTPURLResponse, successRange.contains(response.statusCode) {
guard let weatherInformation = try? decoder.decode(WeatherInformation.self, from: data) else { return }
DispatchQueue.main.async {
self?.weatherStackView.isHidden = false
self?.configureView(weatherInformation: weatherInformation)
}
} else {
guard let errorMsg = try? decoder.decode(ErrorMsg.self, from: data) else { return }
DispatchQueue.main.async {
self?.showAlert(msg: errorMsg.message)
}
}
}.resume()
}
}
728x90
LIST
'SWIFT' 카테고리의 다른 글
Status of COVID-19 outbreak(CocoaPods, Alamfire, Charts) (0) | 2022.12.17 |
---|---|
Do not Work pod init in workspace (0) | 2022.12.17 |
Pomodoro Timer (0) | 2022.12.15 |
To Do List (0) | 2022.12.14 |
Basic Calculator (0) | 2022.12.13 |