Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

SwiftbeginnercliAI generated

Command-Line Unit and Currency Converter

Build a command-line converter in Swift that handles both units and money. You type convert 10 km mi or convert 100 EUR USD and get the answer. Units (length, weight and volume, metric and imperial) are converted with Foundation's built-in Measurement types, and currencies use live exchange rates downloaded with URLSession, saved locally so the tool still works offline with the last known rates. Mixing incompatible things, like kilograms to miles, gives a clear error. It's a nice first Swift project outside of apps: you'll learn Swift packages and command-line tools, Foundation's Measurement API, async/await networking, decoding JSON with Codable, parsing arguments, and handling errors.

Estimate
~9h
Steps
6
Completed by
0
Proposed by
codeseed.app

Foundation · URLSession

Project roadmap

  1. 01

    Create the package

    ~1h

    Create a package with swift package init --type executable, and add Swift Argument Parser as a dependency. Define a command that takes a value and two unit names, with --help. Done when: swift run convert --help shows the usage.

  2. 02

    Convert units

    ~2h

    Map unit names (km, mi, m, ft, kg, lb, l, gal and so on) to Foundation units such as UnitLength.kilometers and UnitMass.pounds. Convert with Measurement(value:unit:).converted(to:). When the two units belong to different kinds, throw an error that names both. Done when: 10 km to mi prints 6.21, and kg to mi gives a clear error.

  3. 03

    Fetch exchange rates

    ~2h

    When both names are 3-letter currency codes, fetch rates from a free API such as Frankfurter with URLSession's async data(from:) and decode the JSON with Codable. Save the rates and their date to a JSON file in the user's application support folder. Done when: 100 EUR USD prints a result using today's rate.

  4. 04

    Work offline

    ~1.5h

    When the request fails (no internet, timeout or an unexpected response), use the saved rates and print a note such as Using rates from 12 March (offline). Refresh saved rates only when they're older than a day. When there are no saved rates and no connection, print a clear error. Done when: converting currencies in airplane mode works with the saved rates.

  5. 05

    Format the output

    ~1h

    Print results like 10 km = 6.21 mi, using MeasurementFormatter or NumberFormatter with sensible decimals, and 2 decimals with the currency code for money. Add --list to show supported units and currencies, and --precision to choose decimals. Done when: the output is clean for every kind of conversion.

  6. 06

    Test it

    ~1.5h

    Write XCTest (or Swift Testing) tests for unit conversions, incompatible units, parsing names, and currency conversion using a fake rates provider instead of the network, including the offline fallback. Done when: swift test passes without the internet.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~9h · 6 steps

Tech stack

FoundationURLSession