测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2017年10月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Juanpe Catalán 维护。
Counter 是一个强大且多功能的计数器。
如果您愿意,可以将 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)
手动结束计数
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
*/
这是一个开源项目,所以您可以随时贡献。怎么办?
查看 所有贡献者
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.