A collection of useful Swift extensions, operators, and utilities to boost your productivity.
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...10Functional programming utilities including the pipe operator |> and identity.
// Pipe operator for chaining
letresult= value |> mapFunction |> anotherFunction
// Handling optionals and throwing functions
letprocessed=try value |> throwingFunctionA 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.missingHelpers 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"
}}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 accessRun 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")}Add this package to your project using Swift Package Manager.
dependencies:[.package(url:"https://github.com/your-username/Swiftic.git", from:"1.0.0")]- Swift 6.0+
- macOS 14.0+
- iOS 16.0+
- watchOS 9.0+
- tvOS 16.0+
This library is released under the MIT license. See LICENSE for details.