Skip to content

Latest commit

 

History

History
115 lines (84 loc) · 4.17 KB

README.md

File metadata and controls

115 lines (84 loc) · 4.17 KB

OSVer

SwiftPM Twitter GitHub GitHub issues GitHub Workflow Status

Codecov CodeFactor Grade codebeat badge Code Climate maintainability Code Climate technical debt Code Climate issues

A Swift package that provides a lightweight, safe wrapper around Foundation.OperatingSystemVersion with additional features like JSON encoding/decoding and string parsing.

Features

  • ✨ Codable support with flexible encoding formats (string or object)
  • 🔒 Type-safe initialization and comparison
  • 📝 String parsing and formatting
  • 🎯 Swift Package Manager support
  • 💪 Fully tested with unit and fuzzy tests

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/yourusername/OSVer.git", from: "1.0.0")
]

Usage

Basic Initialization

// Initialize with major and minor versions (patch defaults to 0)
let version1 = OSVer(major: 1, minor: 2)

// Initialize with all components
let version2 = OSVer(major: 1, minor: 2, patch: 3)

// Initialize using OperatingSystemVersion style
let version3 = OSVer(majorVersion: 1, minorVersion: 2, patchVersion: 3)

// Initialize from OperatingSystemVersion
let osVersion = OperatingSystemVersion(majorVersion: 1, minorVersion: 2, patchVersion: 3)
let version4 = OSVer(osVersion)

String Parsing

// Parse from string
let version = try OSVer(string: "1.2.3")

// Parse without patch version (defaults to 0)
let version = try OSVer(string: "1.2") // equivalent to "1.2.0"

JSON Encoding/Decoding

// Encode as object (default)
let encoder = JSONEncoder()
let data = try encoder.encode(version)
// Result: {"major":1,"minor":2,"patch":3}

// Encode as string
encoder.userInfo[OSVer.encodingFormatKey] = OSVer.EncodingFormat.string
let stringData = try encoder.encode(version)
// Result: "1.2.3"

// Decode from either format
let decoder = JSONDecoder()
let version = try decoder.decode(OSVer.self, from: jsonData)

Comparison

let v1 = OSVer(major: 1, minor: 2)
let v2 = OSVer(major: 1, minor: 3)

// Compare versions
if v1 < v2 {
    print("Version 1 is older")
}

// Use in collections
let versions: Set<OSVer> = [v1, v2]
let versionMap: [OSVer: String] = [
    v1: "Initial Release",
    v2: "Feature Update"
]

Requirements

  • Swift 5.9+
  • iOS 17.0+ / macOS 14.0+ / tvOS 17.0+ / watchOS 10.0+

License

This library is released under the MIT license. See LICENSE for details.