SwiftFoundationExtensions 库

GitHub

一个开源库,为核心 Foundation Swift 库提供了一些有用的扩展和实用程序。作为 XII 的 iOS、macOS 和 watchOS 应用程序中各种项目的可重用组件而开发。

安装

Swift Package Manager

  1. 在 Xcode 中,选择 “File” > “Swift Packages” > “Add Package Dependency”。
  2. 按照提示使用此仓库的 URL
  3. 选择 SwiftFoundationExtensions 库添加到您的项目中

许可证

请参阅 LICENSE 文件。

包含的扩展 / API

信息 (Source)

let mainBundleVersion: String

主 Bundle 的完整版本和构建号,格式为:v[MAJOR].[MINOR].[PATCH]-[BUILD]


let isInstalledFromAppstore: Bool

如果主 Bundle 是从 App Store 安装的(不在沙盒环境中),则为 True,否则为 False。

Date 扩展 (Source)

var startOfWeek: Date { get }

var daysRemainingThisWeek: Int { get }

var startOfMonth: Date { get }

var startOfYear: Date { get }

init(year: Int, month: Int, day: Int)

Comparable 扩展 (Source)

func clamped(to limits: ClosedRange<Self>) -> Self

func clamped(to limits: Range<Self>) -> Self where Self : Strideable

Double 扩展 (Source)

func isCloseTo(_ other: Double) -> Bool

func isCloseToOrGreaterThan(_ other: Double) -> Bool

func isCloseToOrLessThan(_ other: Double) -> Bool

var roundedUp: Int { get }

var roundedDown: Int { get }

Sequence 扩展 (Source)

func sum() -> Double where Element == Double

func sumOfSquares() -> Double where Element == Double

func sum() -> Int where Element == Int

func sumOfSquares() -> Int where Element == Int

func asDictionary<Key : Hashable, Value>() -> [Key: Value]
  where Element == (Key, Value)

func mapToDictionary<Key : Hashable, MappedValue>(
  _ pairExtractor: @escaping (Element) throws -> (Key, MappedValue)
) rethrows -> [Key: MappedValue]

func grouped<Key : Hashable>(
  _ keyExtractor: @escaping (Element) throws -> Key
) rethrows -> [Key: [Element]]

EnumeratedSequence 扩展 (Source)

func mapToDictionary<Key : Hashable, MappedValue>(
  _ pairExtractor: @escaping (Iterator.Element) throws -> (Key, MappedValue)
) rethrows -> [Key: MappedValue]

Collection 扩展 (Source)

func average() -> Double where Element == Double

func average() -> Int where Element == Int

AsyncSequence 扩展 (Source)

func sum() async rethrows -> Double where Element == Double

func sumOfSquares() async rethrows -> Double where Element == Double

func sum() async rethrows -> Int where Element == Int

func sumOfSquares() async rethrows -> Int where Element == Int

Dictionary 扩展 (Source)

mutating func increment(key: Key, by value: Value) where Value == Int

mutating func increment(key: Key) where Value == Int

mutating func decrement(key: Key) where Value == Int

mutating func increment(key: Key, by value: Value) where Value == Double

mutating func increment(key: Key) where Value == Double

mutating func decrement(key: Key) where Value == Double

mutating func incrementSubMapValue<SubKey : Hashable>(
  key: Key,
  subKey: SubKey,
  by value: Int
) where Value == Dictionary<SubKey, Int>

mutating func incrementSubMapValue<SubKey : Hashable>(
  key: Key,
  subKey: SubKey
) where Value == Dictionary<SubKey, Int>

mutating func decrementSubMapValue<SubKey : Hashable>(
  key: Key,
  subKey: SubKey
) where Value == Dictionary<SubKey, Int>

mutating func incrementSubMapValue<SubKey : Hashable>(
  key: Key,
  subKey: SubKey,
  by value: Double
) where Value == Dictionary<SubKey, Double>

mutating func incrementSubMapValue<SubKey : Hashable>(
  key: Key,
  subKey: SubKey
) where Value == Dictionary<SubKey, Double>

mutating func decrementSubMapValue<SubKey : Hashable>(
  key: Key,
  subKey: SubKey
) where Value == Dictionary<SubKey, Double>

mutating func append<InnerValue>(
  value: InnerValue,
  for key: Key
) where Value == Array<InnerValue>

func inverted() -> [Value: Key] where Value : Hashable

func inverted() -> [Value.Element: [Key]]
  where Value : Sequence, Value.Element : Hashable

mutating func set<SubKey : Hashable, SubValue>(
  value: SubValue,
  key: Key,
  subKey: SubKey
) where Value == Dictionary<SubKey, SubValue>

Logger 扩展 (Source)

static func loggerFor(_ category: String) -> Logger

static func loggerFor<Type>(_ type: Type.Type) -> Logger

ErrorWithTrace 结构体 (Source)

一种特殊的 Error,用于捕获抛出错误的 文件/行号,以帮助调试。

遵循协议:ErrorCustomStringConvertible

let file: String

let line: Int

let error: Error

var description: String { get }

var localizedDescription: String { get }

Error 扩展 (Source)

var description: String { get }

func withTrace(filePath: String = #file, line: Int = #line) -> ErrorWithTrace

func castError<Target : Error>(to type: Target.Type) -> Target?

ElapsedTimer 实用程序 (Source)

一个用于跟踪/报告操作之间经过时间的实用程序。

利用 CFAbsoluteTimeGetCurrent 获取设备上当前时间的精确测量值,并将这些测量值之间的差异作为经过时间。

init()

func reset()

func elapsedTimeSeconds() -> Double

func elapsedTimeSecondsText() -> String

func elapsedTimeSecondsAndReset() -> Double

func elapsedTimeSecondsTextAndReset() -> String