Skip to content

1.2 – Wrapping

Choose a tag to compare

@KyNorthstar KyNorthstar released this 30 Aug 18:42
· 3 commits to production since this release
ff4ca8f

So you want your value to be within a particular range, but instead of clamping when it gets outside that range, you want it to repeat back to the beginning of the range?

Gotcha Covered

Now that's as simple as calling .wrapped(within:) on any number!

value.wrapped(within: range)

This loops the number within the range, like this:

A chart depicting a sawtooth-style wrapping of numbers, 3.0 through 4.9, repeating left to right

Here's a code example of how this works:

print(0.wrapped(within: 3..<6)) // 3
print(1.wrapped(within: 3..<6)) // 4
print(2.wrapped(within: 3..<6)) // 5
print(3.wrapped(within: 3..<6)) // 3
print(4.wrapped(within: 3..<6)) // 4
print(5.wrapped(within: 3..<6)) // 5
print(6.wrapped(within: 3..<6)) // 3
print(7.wrapped(within: 3..<6)) // 4
print(8.wrapped(within: 3..<6)) // 5

Patches

  • 1.2.1 • #10 • Fixed compiler error when archiving