swift-open-weather

main

Open Weather API implementation
nut-code-monkey/swift-open-weather

OpenWeather Swift SDK

A lightweight, async‑await‑based Swift wrapper for the OpenWeather API.
It provides type‑safe models for current weather, 5‑day forecasts, and air‑pollution data, along with convenient enums for units and language selection.

Features

  • ✅ Modern Swift
  • ✅ Automatic conversion of timestamps to Date objects
  • ✅ Built‑in support for all OpenWeather language codes and units (metric, imperial, standard)

Requirements

  • Swift 5.8 or later
  • iOS 13+, macOS 10.15+, watchOS 6+, tvOS 13+ (any platform that supports Swift Concurrency)
  • An OpenWeather API key

Installation

Swift Package Manager

Add the package to your project’s Package.swift or via Xcode:

.package(url: "https://github.com/your‑username/swift-open-weather.git", from: "1.0.0")


 .target(
    name: ....,
    dependencies: [.product(name: "OpenWeather", package: "OpenWeatherSwiftSDK")]
)

Quick start

import OpenWeather

// 1️⃣ Create the client with your API key
let weatherClient = OpenWeather(apiKey: "YOUR_API_KEY")

// 2️⃣ Fetch the current weather for a location
do {
    let current = try await weatherClient.current(
        lat: 37.7749,
        lon: -122.4194,
        lang: .English,                 // optional – defaults to English
        units: .currentLocale           // optional – defaults to locale‑aware units
    )
    
    print("Temperature: \(current.main.temp)°")
    print("Condition: \(current.weather.first?.label ?? "N/A")")
    print("Icon URL: \(current.weather.first?.iconUrl?.absoluteString ?? "none")")
} catch {
    print("❌ Failed to fetch weather:", error)
}

Forecast Example (5‑day / 3‑hourly)

do {
    let forecast = try await weatherClient.forecast(
        lat: 37.7749,
        lon: -122.4194
    )
    // Each `Item` represents a 3‑hour block
    for item in forecast.list.prefix(5) {
        print("\(item.date): \(item.main.temp)°, \(item.weather.first?.label ?? "")")
    }
} catch {
    print("❌ Forecast error:", error)
}    

Air Pollution Example

do {
    let air = try await weatherClient.airPollutions(
        lat: 37.7749,
        lon: -122.4194
    )
    if let first = air.list.first {
        print("AQI: \(first.main.aqi) – \(first.main.airQualityIndex?.description ?? "unknown")")
        print("Components: CO \(first.components.co) µg/m³, PM2.5 \(first.components.pm2_5) µg/m³")
    }
} catch {
    print("❌ Air pollution error:", error)
}

Attribution

OpenWeather data is provided under the OpenWeather terms of service. When displaying weather information, include the required attribution:

let attribution = OpenWeather.attribution
print("\(attribution.title) – \(attribution.url)")

Description

  • Swift Tools 6.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Nov 15 2026 16:58:53 GMT-1000 (Hawaii-Aleutian Standard Time)