Skip to content

Latest commit

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Swiftic

A collection of useful Swift extensions, operators, and utilities to boost your productivity.

Features

1. Comparable Extensions

Easily clamp or bound values within ranges.

// Clamping to a closed range
42.clamped(to:0...100) // -> 42
Int.min.clamped(to:0...100) // -> 0
Int.max.clamped(to:0...100) // -> 100
// Clamping to partial ranges
42.clamped(to:0...) // -> 42
42.clamped(to:...100) // -> 42
// Bounding between two values (handles unordered bounds)
val.bounded(between:10, and:5) // Equivalent to clamping to 5...10

2. Functional Helpers

Functional programming utilities including the pipe operator |> and identity.

// Pipe operator for chaining
letresult= value |> mapFunction |> anotherFunction
// Handling optionals and throwing functions
letprocessed=try value |> throwingFunction

3. Optional Handling

A concise way to unwrap an optional or throw an error if nil.

letvalue:Int?=nilenumMyError:Error{case missing }
// Throws MyError.missing because value is nil
letunwrapped=try value ??MyError.missing

4. Development Utilities

Helpers for marking incomplete code or impossible states.

func implementMeLater(){todo("Need to implement this feature") // Fatal error with "[TODO]: Need to implement this feature"
}func handleEnum(_ value:MyEnum){switch value {case.a:print("a")case.b:print("b")@unknowndefault:unreachable() // Fatal error with "UNREACHABLE"
}}

5. TypedDictionary

A type-safe wrapper around dictionaries using phantom types.

// 1. Define your Namespace
structUserInfo{typealiasProperty=ScopedProperty<Self>}
// 2. Define keys
extensionUserInfo.Property{staticletname=Key<String>(rawValue:"name")staticletage=Key<Int>(rawValue:"age")}
// 3. Usage
vardic=TypedDictionary<UserInfo>()dic[.name]="John"dic[.age]=30letname:String?=dic[.name] // Type-safe access

6. Task Timeout

Run async work with a hard timeout using either a duration or an absolute deadline.

do{
_ =tryawaitTask.timeout(for:.milliseconds(250)){tryawaitslowOperation()}}catch is TimedOutError{print("Operation timed out")}

Installation

Add this package to your project using Swift Package Manager.

dependencies:[.package(url:"https://github.com/your-username/Swiftic.git", from:"1.0.0")]

Requirements

  • Swift 6.0+
  • macOS 14.0+
  • iOS 16.0+
  • watchOS 9.0+
  • tvOS 16.0+

License

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages