A simple Swift package that provides an observable counter class for SwiftUI applications.
- Observable Counter: Built on
ObservableObjectfor seamless SwiftUI integration - Flexible Operations: Increment/decrement by 1 or custom amounts
- Reset Functionality: Reset to zero or any specific value
- Thread-Safe: Uses
@Publishedproperty wrapper for reactive updates
- iOS 13.0+ / macOS 10.15+
- Swift 6.1+
Add CounterPlus as a dependency in your Package.swift:
dependencies: [
.package(url: "https://github.com/chornthorn/CounterPlus.git", from: "1.0.0")
]Or add it to your Xcode project:
- Go to File → Add Packages...
- Enter the repository URL:
https://github.com/chornthorn/CounterPlus.git - Choose the version you want to use
import SwiftUI
import CounterPlus
struct ContentView: View {
@StateObject private var counter = Counter(initialValue: 5)
var body: some View {
VStack(spacing: 20) {
Text("Count: \(counter.value)")
.font(.largeTitle)
HStack(spacing: 20) {
Button("-") {
counter.decrement()
}
Button("+") {
counter.increment()
}
}
Button("Reset") {
counter.reset()
}
}
.padding()
}
}increment()- Increments by 1increment(by: Int)- Increments by specified amountdecrement()- Decrements by 1decrement(by: Int)- Decrements by specified amountreset()- Resets to 0reset(to: Int)- Resets to specified valuevalue- Read-only property to get current count
Check out the Example/ directory for a complete SwiftUI demo application.