Counter is a Swift Package Manager package for iOS/tvOS (10.0 and above), watchOS (4.0 and above), and macOS (10.14 and above), under Swift 5.0 and above, defining a model type to represent an integer counter:
publicenumCountingAction:String,Hashable,CaseIterable,Codable{case increment
case decrement
}publicstructCounter<CountType:FixedWidthInteger, StepType:UnsignedInteger&FixedWidthInteger>{publicletstep:StepTypepublicletminCount:CountTypepublicletmaxCount:CountTypepublicinternal(set)varcount:CountTypepublicvarisEnabled:Boolpublicinit(
step:StepType=1, // max(1, step)
minCount:CountType=.zero,
maxCount:CountType=.max,
count:CountType=.zero, // clamped to the range [minCount, maxCount]
isEnabled:Bool=true)
/// Increments the counter by its `step` value, if incrementing is enabled.
/// Otherwise, does nothing.
publicmutatingfunc increment()
/// Decrements the counter by its `step` value, if decrementing is enabled.
/// Otherwise, does nothing.
public mutatingfunc decrement()
/// Incrementing is enabled when the counter itself is enabled **and** incrementing
/// the counter's current value will not go above its maximum value.
publicvarisIncrementingEnabled:Bool
/// Decrementing is enabled when the counter itself is enabled **and** decrementing
/// the counter's current value will not go below its minimum value.
publicvarisDecrementingEnabled:Bool}extensionCounter:Equatablewhere CountType:Equatable, StepType:Equatable{}extensionCounter:Hashablewhere CountType:Hashable, StepType:Hashable{}extensionCounter:Codablewhere CountType:Codable, StepType:Codable{}Note that minCount need not be .zero. It can be as low as .min.
Counter is provided only as a Swift Package Manager package, because I'm moving away from CocoaPods and Carthage, and can be easily installed directly from Xcode.
Counter is available under the MIT license. See the LICENSE file for more info.