SwiftFormats

为更多类型提供更多 FormatStyle 实现。

描述

此包为各种类型提供了更多 FormatStyle 实现。许多类型还提供了 parserStrategy 实现,以便在适当的情况下将字符串解析为该类型。

它还为 String 和字符串插值提供了扩展,以便更轻松地格式化值。

类型

名称 输入 (1) 输出 (2) 格式 (3) 解析器 (4) 访问器 (5) 注释
BinaryFloatingPoint String 角度 angle 弧度、度等
BinaryFloatingPoint String 度分秒 dmsNotation
CGPoint String 列表 (6) point
ClosedRange String X ... Y
CLLocationCoordinate2D String 列表 coordinates
BinaryFloatingPoint String 纬度 latitude 包括半球
BinaryFloatingPoint String 经度 longitude 包括半球
Any String 描述 describing 使用 String(describing:)
Any String Dump dumped 使用 dump()
DataProtocol String Hex-dumped hexdumped
Codable String JSON json 使用 JSONEncoderJSONDecoder
SIMD3<Float> String 列表或映射 vector
SIMD 矩阵类型 String 列表 matrix
BinaryInteger String Radixed 格式 各种 二进制、八进制、十六进制表示

注释

1:在 FormatStyle 实现中作为 FormatInput 提供的类型。2:在 FormatStyle 实现中作为 FormatOutput 提供的类型。3:输出的格式。4:FormatStyle 实现是否提供相应的 ParserStrategy。5:是否提供了方便的属性来访问 FormatStyle 上的样式。6:将输入格式化为逗号分隔的列表。

示例

字符串插值

let number = 123.456
let formatted = "The number is \(number, .number)"
// formatted == "The number is 123.456"

CoreGraphics

let point = CGPoint(x: 1.234, y: 5.678)
let formatted = point.formatted(.decimal(places: 2))
// formatted == "(1.23, 5.68)"

未来计划

最初的优先级是为更多 SIMD/CG 类型公开格式和解析器。将添加一些常见的辅助格式样式(例如,“field”解析器 - 参见 TODO)。

TODO

资源

Apple

其他