TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Sep 2016 |
SPMSupports SPM | ✗ |
Maintained by Yaxin Cheng.
WeatherKit is simple kit for weather information in Swift
WeatherKit can be installed through Cocoapods or manual import.
Kit imports
import WeatherKit
WeatherStation is the interface which retrieves weather/forecast information
The contructor accepts 4 units parameters with default values, which will be used to convert weather/forecast information
public init(temperatureUnit: TemperatureUnit = .celsius, distanceUnit: DistanceUnit = .mi, directionUnit: DirectionUnit = .direction, speedUnit: SpeedUnit = .mph)
let station = WeatherStation()
station.weather(location: someLocation) { result in
switch result {
case .Success(let json): // Get a single weather json
// Do something
case .Failure(let error):
// Error handling
}
}
station.forecast(location: someLocation) { result in
switch result {
case .Success(let jsons): // Get an array of forecast json
// Do something
case .Failure(let error):
// Error handling
}
}
With default values for province and country, so the two parameters can be skipped
But searching with those two parameters can increase the accuracy of search
let station = WeatherStation()
station.weather(city: "cityName", province: "provinceName", country: "countryName") { result in
switch result {
case .Success(let json): // Get a single weather json
// Do something
case .Failure(let error):
// Error handling
}
}
station.weather(city: "cityName") { result in // The province and country can be skipped
switch result {
case .Success(let json): // Get a single weather json
// Do something
case .Failure(let error):
// Error handling
}
}
station.forecast(city: "cityName", province: "provinceName", country: "countryName") { result in
switch result {
case .Success(let jsons): // Get an array of forecast json
// Do something
case .Failure(let error):
// Error handling
}
}
There are two JSON types: weather& forecast. The details are listed below
/*weather json*/
weatherJSON["temperature"] // Double value about the weather temperature
weatherJSON["condition"] // String value about the weather condition
weatherJSON["conditionCode"] // Int value, code of the condition
weatherJSON["windChill"] // Double value about the wind temperature
weatherJSON["windSpeed"] // Double value about the wind speed
weatherJSON["windDirection"] // Double value about the direction of winds
weatherJSON["humidity"] // String value about the humidity
weatherJSON["visibility"] // Double value about the visibility
weatherJSON["pressure"] // String value about the pressure
weatherJSON["trend"] // String value about the pressure trend
weatherJSON["sunrise"] // NSDateComponents value
weatherJONS["sunset"] // NSDateComponents value
/*forecast json*/
forecastJSON["high"] // Double value about the highest temperature
forecastJSON["low"] // Double value about the lowest temperature
forecastJSON["date"] // String value about the forecast date
forecastJSON["text"] // String value about the forecast condition
forecastJSON["day"] // String value about the weekday the forecast is for
forecastJSON["code"] // String value about the weather condition code
By default, weather station keeps caches which speed up the loading process
Normally, caches will be replaced and auto-cleared
But weather station also offers the option to clear cache manually
let station = WeatherStation()
station.clearCache()
CityLoader is the main access for loading city information and parsing location information
The constructor accepts no parameters, which makes CityLoader easier to be instanced
With default values for province and country, so the two parameters can be skipped
But searching with those two parameters can increase the accuracy of search
public init() { ... }
let loader = CityLoader()
loader.loadCity(city: "cityName", province: "provinceName", country: "countryName") { result in
// result is an array of city JSONs
// If no city matches the description, or any error happens, result is empty
}
Another way to load a city is searching by WOEID, which is a unique value used to locate a city
let loader = CityLoader()
loader.loadCity(woeid: "SomeWOEID") { result in
// result is an optional typed JSON (Dictionary<String, AnyObject>?)
// If no city matches the woeid, or any error happens, then the result == nil
}
#### Load sunrise& sunset information:
City loader can also check sunrise& sunset time for a city with the woeid, the result will be given back as a tuple of two optional NSDateComponents <br>
``` swift
let loader = CityLoader()
loader.dayNight(woeid: "someWOEID") { sunrise, sunset in
// sunrise and sunset can be nil
}
City loader can parse the information of a CLLocation value, and return a city json back.
let loader = CityLoader()
loader.locationParse(location: someLocation) { city in
// city can be nil
// if city is not nil, city is a Dictionary<String, AnyObject>
}
Below is the content of a city json
cityJSON["name"] //String value about the name of the city
cityJSON["admin1"] //String value about the name of the province or state
cityJSON["country"] //String value about the name of the country
cityJSON["woeid"] //String value about the woeid of the city
cityJSON["centroid"]?["latitude"] //String value about the latitude information of the city
cityJSON["centroid"]?["longitude"] //String value about the longitude information of the city
cityJSON["timezone"] //String value about the timezone label of the city
WeatherKit is released under the MIT license. See LICENSE for details.