Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chornthorn/CounterPlus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CounterPlus

A simple Swift package that provides an observable counter class for SwiftUI applications.

Features

  • Observable Counter: Built on ObservableObject for seamless SwiftUI integration
  • Flexible Operations: Increment/decrement by 1 or custom amounts
  • Reset Functionality: Reset to zero or any specific value
  • Thread-Safe: Uses @Published property wrapper for reactive updates

Requirements

  • iOS 13.0+ / macOS 10.15+
  • Swift 6.1+

Installation

Swift Package Manager

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:

  1. Go to File → Add Packages...
  2. Enter the repository URL: https://github.com/chornthorn/CounterPlus.git
  3. Choose the version you want to use

Usage

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()
    }
}

Available Methods

  • increment() - Increments by 1
  • increment(by: Int) - Increments by specified amount
  • decrement() - Decrements by 1
  • decrement(by: Int) - Decrements by specified amount
  • reset() - Resets to 0
  • reset(to: Int) - Resets to specified value
  • value - Read-only property to get current count

Example Project

Check out the Example/ directory for a complete SwiftUI demo application.

About

This is for learning purpose only.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages