测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最新发布 | 2017年1月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✓ |
Miran Brajsa 维护。
此包包含我们倾向于在我们的众多项目中重用的 Swift 工具函数(我们是Five Agency 的 iOS 团队)。我们预计还会有更多添加,但在此期间,您可以自由使用/重用/升级这里现有的所有代码。一切有关辅助函数、随机生成器、数学和其他有用的代码片段。
Swift 3.0
iOS 8.0+
没有额外的依赖项。
当前支持的安装选项
创建一个 Package.swift
文件。
import PackageDescription
let package = Package(
name: "FiveUtilsTestProject",
targets: [],
dependencies: [
.Package(url: "https://github.com/fiveagency/ios-five-utils", majorVersion: 1)
]
)
$ git submodule add https://github.com/fiveagency/ios-five-utils.git
将 FiveUtils.xcodeproj
拖到项目导航器中。
转到 项目 > 目标 > 构建阶段 > 链接到库 Binary
,点击 +
并选择 FiveUtils.framework
目标。
要运行示例项目,请执行以下操作。
$ git clone https://github.com/fiveagency/ios-five-utils.git YOUR_DESTINATION_FOLDER
FiveUtils.xcworkspace
,选择 Example
方案并按 run
。此方法将构建所有内容并运行示例应用。/**
Clamps value between lower and upper bound.
*/
public static func clamp<T: Comparable>(_ value: T, between lower: T, and upper: T) -> T
示例使用
print(clamp(32, lower: 1, upper: 3))
print(clamp(14, lower: 10, upper: 20))
print(clamp(1, lower: 5, upper: 7))
结果是
> 3
> 14
> 5
/**
Interpolates linearly between initial and target value.
*/
public static func lerp<T: Lerpable>(from first: T, to second: T, t: CGFloat) -> T
注意:目前支持的 lerpable 类型是 CGFloat
、Double
、Float
和 Int
。
示例使用
print(lerp(from: 4, to: 8, t: 0.5))
print(lerp(from: 1.0, to: 5.0, t: 1.0))
结果是
> 6
> 5.0
/**
Enables us to compare floating point numbers within a given precision frame.
*/
protocol FloatingPointComparable: FloatingPoint {
func isEqualEnough(to other: Self, withPrecision precision: Double) -> Bool
func typeBasedPrecision(fromPrecision precision: Double) -> Self
}
示例使用
print(Float(15).isEqualEnough(to: 15.002, withPrecision: 0.001))
print(Float(15.0025).isEqualEnough(to: 15.002, withPrecisionScale: 0.001))
print(Float(15).isEqualEnough(to: 15.002))
结果是
> false
> true
> false
注意:可以使用没有精度缩放参数的函数的简短版本。默认的是一个 0.001。 注意:目前支持的 lerpable 类型是 CGFloat
、Double
和 Float
。
/**
Returns a random CGFloat value specified in the [from, to) set. If no values get specified,
the default set is [0.0, 1.0). The method allows for set bounds to be inverted meaning
upper set bound may be set to smaller number than lower bound.
*/
public static func random(_ from: CGFloat = 0, to: CGFloat = 1.0) -> CGFloat
示例使用
print(random())
print(random(3.0, to: 4.5))
print(random(2.0, to: 0.0))
可能的结果是
> ~0.76
> ~3.78
> ~1.15
类似地,本模块中还有其他随机生成器
public static func from(_ range: Range<Int>) -> Int
public static func from(_ from: Int, to: Int) -> Int
public static func alphanumericCharacter() -> Character
public static func alphanumericString(withLength length: Int) -> String
/**
Creates an FiveTimeInterval object by summing up amounts of different time units.
*/
public init(weeks: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Double = 0)
示例使用
let timeInterval = TimeInterval(weeks: 0, days: 0, hours: 0, minutes: 2)
print(timeInterval.totalSeconds)
print(timeInterval.totalMinutes)
结果是
> 120
> 2
/**
Wrapper around UIApplication network activity indicator.
It ensures that activity indicator cannot be hidden if there is some active task that
requires its presence. Also, when all tasks are done, it automatically hides network
activity indicator.
*/
示例使用
let networkActivityController = FiveNetworkActivityController.shared
// Start two network tasks (usually called by some outer handler module)
// and display the network activity indicator.
networkActivityController.startActivity()
networkActivityController.startActivity()
networkActivityController.endActivity() // Here, the network activity indicator is still displayed.
networkActivityController.endActivity() // Finally, hide the activity indicator.
/*
Allows for returning of 'nil' values in case a specified index is out of bounds.
*/
public subscript(safe index: Int) -> Element?
示例使用
let dummyArray = [1, 2, 3, 4, 5]
print(dummyArray[safe: 1])
print(dummyArray[safe: 13])
结果是
> 2
> nil
/**
Returns a random element.
*/
public func randomElement() -> Element
示例使用
let dummyArray = [1, 2, 3, 4, 5]
print(dummyArray.randomElement())
可能的结果是
> 4
以下两个与通过 NSKeyedArchiver/Unarchiver 存档数组相关。
/**
Archives the array into user defaults.
*/
public func archive(_ key: String)
/**
Tries to unarchive the array. If unsuccessfull, returns nil.
*/
public static func unarchive(_ key: String) -> Array?
示例使用
let key = "archiveKey"
let dummyArray = [1, 2, 3, 4, 5]
dummyArray.archive(key)
guard let unarchivedArray = Array<Int>.unarchive(key) else {
assertionFailure("This should not happen.")
}
print(unarchivedArray == dummyArray)
结果是
> true
/**
Returns a string that represents this integer as an ordinal number, e.g. "1st" for 1, "2nd" for 2 etc.
*/
public var ordinalString: String
示例使用
print(1.ordinalString)
print(2.ordinalString)
print(3.ordinalString)
print(4.ordinalString)
print(92.ordinalString)
结果是
> 1st
> 2nd
> 3rd
> 4th
> 92nd
Five Utils 库团队(按字母顺序排列)
Denis Mendica
Kristijan Rožanković
Miran Brajsa
Niko Mikuličić
FiveUtils 受 MIT 协议许可。有关更多信息,请参阅LICENSE 文件。