Counter 1.1

Counter 1.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年10月
SwiftSwift 版本3.0
SPM支持 SPM

Juanpe Catalán 维护。



Counter 是一个强大且多功能的计数器。

功能🚀

  • 易于使用
  • 增加/减少
  • 您决定起始值
  • 在单个调用中添加多个值
  • 使用您自己的对象
  • 设置里程碑(警报)
  • 自动计数器

支持的 OS 及 SDK 版本📋

  • iOS 9.0+
  • Swift 3

安装📲

手动 💪🏼

如果您愿意,可以将 Counter 集成到您的项目中。只需直接将 Counter.swift 源文件添加到您的项目中即可。

如何使用🐒

在适当的位置导入 Counter。

import Counter

现在,您只需要创建一个 Counter 实例并开始使用🙂

let counter = Counter() // 0
counter.increment()     // 1

您可以指定要增加计数器的单元数

counter.increment(2)    // 3

您也可以减少计数

counter.decrement()     // 2

当然,您可以重置计数器

counter.reset()         // 0

其他强大的功能是您可以在同一时间添加多个值

counter.sum(countables: 2, 3, -1)  // 4

高级🤓

可以使用自己的对象来增加计数器。只需实现 Countable 协议即可

protocol Countable {
    var deltaValue: Int { get }
}

示例

struct Person {
    var age: Int
}

extension Person: Countable{
    var deltaValue: Int {
        return self.age
    }
}

let dad = Person(age: 45)
let mum = Person(age: 40)
let son = Person(age: 25)

let ageCounter = Counter()
ageCounter.increment(dad)
ageCounter.increment(mum)
ageCounter.increment(son)

print(ageCounter.currentValue) // 110

// Also you can use `sum` method or static method

ageCounter.sum(countables:dad, mum, son) // 110

let ages = Counter.sum(countables:dad, mum, son) // 110

里程碑🔔

有时您需要知道计数器何时达到了或将要达到某个值。使用 Counter 将变得轻而易举。
您只需要添加 milestones 并遵循 CounterDelegate 协议。然后,计数器将在达到每个先前定义的里程碑之前和达到时通知您。

protocol CounterDelegate{
    func counter(_ counter: Counter, willReachValue value: Int)
    func counter(_ counter: Counter, hasReachedValue value: Int)
    func counter(_ counter: Counter, didChangeValue value: Int)
}

要添加新的里程碑

counter.add(milestone: 3)

自动Counter

手动结束计数

let automaticCounter = AutomaticCounter(startIn: 0) // takes default parameters (interval: 1, autoIncrement: 1)
automaticCounter.delegate = self
automaticCounter.automaticDelegate = self
automaticCounter.startCounting()

DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
    automaticCounter.endCounting()
}

/* output
counter(_:didChangeValue:) => 1
counter(_:didChangeValue:) => 2
counter(_:didChangeValue:) => 3
counter(_:didFinishCounting:) => 3
*/

在特定值结束时结束计数

let automaticCounter = AutomaticCounter(startIn: 0, interval: 0.5, autoIncrement: 1)
automaticCounter.delegate = self
automaticCounter.automaticDelegate = self
automaticCounter.startCounting(endingAt: 10)

/* output
counter(_:didChangeValue:) => 1
counter(_:didChangeValue:) => 2
counter(_:didChangeValue:) => 3
counter(_:didChangeValue:) => 4
counter(_:didChangeValue:) => 5
counter(_:didChangeValue:) => 6
counter(_:didChangeValue:) => 7
counter(_:didChangeValue:) => 8
counter(_:didChangeValue:) => 9
counter(_:didChangeValue:) => 10
counter(_:didFinishCounting:) => 10
*/

在时间间隔后结束计数

let automaticCounter = AutomaticCounter(startIn: 0, interval: 1)
automaticCounter.delegate = self
automaticCounter.automaticDelegate = self
automaticCounter.startCounting(endingAfter: 3)

/* output
counter(_:didChangeValue:) => 1
counter(_:didChangeValue:) => 2
counter(_:didChangeValue:) => 3
counter(_:didFinishCounting:) => 3
*/

贡献❤️

这是一个开源项目,所以您可以随时贡献。怎么办?

  • 打开 问题
  • 通过 电子邮件 发送反馈。
  • 提出您的自己的修复、建议,并通过包含更改的拉取请求。

查看 所有贡献者

作者 👨🏻‍💻

  • Juanpe Catalán alt text

许可证 👮🏻

MIT License

Copyright (c) 2017 swift-code

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.