Book Image

Swift by Example

By : Giordano Scalzo
Book Image

Swift by Example

By: Giordano Scalzo

Overview of this book

Table of Contents (15 chapters)

Retrieving the actual forecast


We have almost completed the app, but it is still missing the most important part—the weather forecast.

Getting the forecast from OpenWeatherMap

There are a plenty of services that provide forecast for free or for a small amount of money.

For our app, we'll use http://openweathermap.org, whose API is free for a small number of calls.

First of all, we create the struct to handle the forecast:

import Foundation
struct WeatherCondition {
    let cityName: String?
    let weather: String
    let icon: IconType?
    let time: NSDate
    let tempKelvin: Double
    let maxTempKelvin: Double
    let minTempKelvin: Double
    
    var tempFahrenheit: Double {
        get {
            return tempCelsius * 9.0/5.0 + 32.0
        }
    }
    
    var maxTempFahrenheit: Double {
        get {
            return maxTempCelsius * 9.0/5.0 + 32.0
        }
    }
    var minTempFahrenheit: Double {
        get {
            return minTempCelsius * 9.0/5.0 + 32.0
        }
    }...