Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----- CityWeatherTableViewController.swift --------
- import UIKit
- class CityWeatherTableViewController: UITableViewController {
- @IBOutlet weak var weatherDataTableView: UITableView!
- let cellReuseIdentifier = "CityPropCellReuseIdentifier"
- let citiesList: [String] = ["Holon", "Paris", "London", "Malaga", "Madrid", "Zanzibar", "Istanbul", "Roma", "Berlin", "Moscow"]
- struct position {
- var city: String
- var description: String
- var temp: String
- var humidity: String
- var windSpeed: String
- }
- let myAPIkey:String = "e726b53e902ebe807d746eb96ffc01b7"
- var citiesProps:[position] = []
- var myURL:String = ""
- override func viewDidLoad() {
- super.viewDidLoad()
- title = "City Weather"
- navigationController?.navigationBar.prefersLargeTitles = true
- setDelegates()
- getCitiesWeatherProps()
- }
- fileprivate func getCitiesWeatherProps() {
- for city in citiesList {
- getCityWeatherData(city: city)
- }
- print(citiesProps)
- }
- fileprivate func getCityWeatherData(city: String) {
- myURL = "https://api.openweathermap.org/data/2.5/weather?q=\(city)&appid=\(myAPIkey)"
- //convert our string to URL
- let url = URL(string: myURL)!
- var newPos: position?
- //create a url async task
- URLSession.shared.dataTask(with: url){
- (data,response,error) in
- //check if we not have error, and if we have, print the error
- if error != nil {
- let myError = error! as NSError
- print (myError)
- } else {
- do {
- let parseData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]
- let currentWeather = parseData!["weather"] as! NSArray
- let currentWeatherData = currentWeather[0] as! [String:Any]
- let currentDescription = currentWeatherData["description"] as! String
- let currentMainData = parseData!["main"] as! [String:Any]
- var currentTemperature = currentMainData["temp"] as! Double
- let currentHumidity = ((currentMainData["humidity"] as! Double) * 1000).rounded(.toNearestOrEven) / 1000
- let currentWindData = parseData!["wind"] as! [String:Any]
- let currentWindSpeed = ((currentWindData["speed"] as! Double) * 1000).rounded(.toNearestOrEven) / 1000
- newPos = position(city: city, description: currentDescription, temp: String(currentTemperature.f2c()), humidity: String(currentHumidity), windSpeed: String(currentWindSpeed))
- print(newPos!)
- print("a")
- self.citiesProps.append(newPos!)
- DispatchQueue.main.async {
- self.weatherDataTableView.reloadData()
- }
- } catch let error as NSError {
- print (error)
- }
- }
- }.resume()
- }
- fileprivate func setDelegates() {
- weatherDataTableView.delegate = self
- weatherDataTableView.dataSource = self
- }
- // MARK: - Table view data source
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return citiesProps.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as! WeatherInfoTableViewCell
- print(9)
- cell.cityNameLabel.text = citiesProps[indexPath.row].city
- cell.humidityLabel.text = citiesProps[indexPath.row].humidity
- cell.tempLabel.text = citiesProps[indexPath.row].temp
- cell.weatherDescriptionLabel.text = citiesProps[indexPath.row].description
- cell.windSpeedLabel.text = citiesProps[indexPath.row].windSpeed
- switch citiesProps[indexPath.row].description {
- case "broken clouds":
- cell.backgroundColor = .systemGray
- case "scattered clouds":
- cell.backgroundColor = .systemGray2
- case "overcast clouds":
- cell.backgroundColor = .systemGray3
- case "light rain":
- cell.backgroundColor = .systemBlue
- case "clear sky":
- cell.backgroundColor = .systemTeal
- default:
- cell.backgroundColor = .systemPink
- }
- return cell
- }
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 200
- }
- }
- extension Double{
- mutating func f2c() -> Double{
- var x: Double
- x = (5000*(self-32))/9
- return x.rounded(.toNearestOrEven) / 1000
- }
- }
- // ----------- WeatherInfoTableViewCell.swift --------
- import UIKit
- class WeatherInfoTableViewCell: UITableViewCell {
- @IBOutlet var cityNameLabel: UILabel! = UILabel()
- @IBOutlet var weatherDescriptionLabel: UILabel! = UILabel()
- @IBOutlet var tempLabel: UILabel! = UILabel()
- @IBOutlet var humidityLabel: UILabel! = UILabel()
- @IBOutlet var windSpeedLabel: UILabel! = UILabel()
- override func awakeFromNib() {
- super.awakeFromNib()
- // Initialization code
- }
- override func setSelected(_ selected: Bool, animated: Bool) {
- super.setSelected(selected, animated: animated)
- // Configure the view for the selected state
- }
- }
Add Comment
Please, Sign In to add comment