WeatherKit 是一个适用于 Swift 的简单天气信息库
WeatherKit 可以通过 Cocoapods 或手动导入安装。
库导入
import WeatherKit
WeatherStation 是一个获取天气/预报信息的接口
构造函数接受4个单位参数,有默认值,这些值将被用于转换天气/预报信息
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
}
}
默认值为省份和国家,因此可以省略这两个参数
但通过这两个参数进行搜索可以增加搜索的准确性
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
}
}
有三种 JSON 类型:weather、forecast 和 city。详情如下
/*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
默认情况下,气象站保持缓存以加快加载过程
通常,缓存将被替换并自动清理
但气象站还提供手动清除缓存的功能
let station = WeatherStation()
station.clearCache()
CityLoader 是加载城市信息和解析位置信息的主要访问方式
构造函数不接受任何参数,这使得 CityLoader 更容易实例化
默认值为省份和国家,因此可以省略这两个参数
但通过这两个参数进行搜索可以增加搜索的准确性
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
}
另一种加载城市的方法是搜索WOEID,这是一个用于定位城市的唯一值
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
}
城市加载器可以解析CLLocation值的信息,并返回一个城市JSON。
let loader = CityLoader()
loader.locationParse(location: someLocation) { city in
// city can be nil
// if city is not nil, city is a Dictionary<String, AnyObject>
}
以下是一个城市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基于MIT许可证发布。详情请见LICENSE文件。