A Swift Property Wrapper for automatically clamping floating-point values within a closed range of [0,1].

UnitInterval: A Swift Property Wrapper

Twitter: @cypher_poet

A Swift Property Wrapper for automatically clamping floating-point values within a closed range of [0,1].


Xcode Projects

Select File -> Swift Packages -> Add Package Dependency and enter https://github.com/CypherPoet/UnitIntervalPropertyWrapper.

Swift Package Manager Projects

You can add UnitIntervalPropertyWrapper as a package dependency in your Package.swift file:

let package = Package(
    dependencies: [
            url: "https://github.com/CypherPoet/UnitIntervalPropertyWrapper",
            .upToNextMinor(from: "0.1.0")

From there, refer to UnitIntervalPropertyWrapper as a "target dependency" in any of your package's targets that need it.

targets: [
        name: "YourLibrary",
        dependencies: [

Then simply import UnitIntervalPropertyWrapper wherever you’d like to use it.


Basic Usage Example:

import UnitIntervalPropertyWrapper

struct AwesomePhoto {
    public var opacity: Double

    public var animationProgress: Double = 0.0

var photo = AwesomePhoto(opacity: 100)

photo.opacity             // 1.0
photo.animationProgress   // 0.0

photo.animationProgress = -2.2

photo.animationProgress  // 0.0

Also, feel free to use this project's Xcode Playground to explore things further.


Contributions to UnitIntervalPropertyWrapper are most welcome. Check out some of the issue templates for more info.

💻 Developing


  • Xcode 12.5+ (Recommended)

📜 Generating Documentation

Documentation is generated by Jazzy. Installation instructions can be found here, and as soon as you have it set up, docs can be generated simply by running jazzy from the command line.

📝 Note that this will only generate the docs folder for you to view locally. This folder is being ignored by git, as an action exists to automatically generate docs at the root level and serve them on the project's gh-pages branch.

🏷 License

UnitIntervalPropertyWrapper is available under the MIT license. See the LICENSE file for more info.


  • Swift Tools 5.4.0
View More Packages from this Author


Last updated: Tue Apr 02 2024 08:47:35 GMT-0900 (Hawaii-Aleutian Daylight Time)