Swift 团队发布了 Swift 标准库预览版(Swift Standard Library Preview),开发者可使用它访问已通过 Swift Evolution 流程并被 Swift 标准库接纳的功能,不过要注意这些新功能尚未作为 Swift 正式版本的一部分提供。
团队已经从最近通过的 SE-0270 提案中为预览版标准库添加了功能,该提案包括对集合子范围的操作,例如subranges (where:)
和 moveSubranges (_??)
,以及支持的RangeSet
类型。
使用预览版标准库
要在 Swift Package Manager 项目中使用标准库包,请将其作为包依赖项和目标依赖项添加到Package.swift
文件中:
1 2 3 4 5 6 7 8 9 10 11 | let package =<span style="color: #000000;"> Package ( name: </span><span style="color: #800000;">"</span><span style="color: #800000;">MyPackage</span><span style="color: #800000;">"</span><span style="color: #000000;">, dependencies: [ .package (url: </span><span style="color: #800000;">"</span><span style="color: #800000;">https://github.com/apple/swift-standard-library-preview.git</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">from</span>: <span style="color: #800000;">"</span><span style="color: #800000;">0.0.1</span><span style="color: #800000;">"</span><span style="color: #000000;">), ], targets: [ .target ( name: </span><span style="color: #800000;">"</span><span style="color: #800000;">MyTarget</span><span style="color: #800000;">"</span><span style="color: #000000;">, dependencies: [.product (name: </span><span style="color: #800000;">"</span><span style="color: #800000;">StandardLibraryPreview</span><span style="color: #800000;">"</span>, package: <span style="color: #800000;">"</span><span style="color: #800000;">swift-standard-library-preview</span><span style="color: #800000;">"</span><span style="color: #000000;">)]), ] )</span> |
以 SE-0270 作为示例:
1 2 3 4 5 6 7 8 | <span style="color: #000000;">import StandardLibraryPreview </span><span style="color: #0000ff;">var</span> numbers = [<span style="color: #800080;">10</span>, <span style="color: #800080;">12</span>, -<span style="color: #800080;">5</span>, <span style="color: #800080;">14</span>, -<span style="color: #800080;">3</span>, -<span style="color: #800080;">9</span>, <span style="color: #800080;">15</span><span style="color: #000000;">] let negatives </span>= numbers.subranges (<span style="color: #0000ff;">where</span>: { $<span style="color: #800080;">0</span> < <span style="color: #800080;">0</span><span style="color: #000000;"> }) </span><span style="color: #008000;">//</span><span style="color: #008000;"> numbers[negatives].count == 3</span> <span style="color: #000000;"> numbers.moveSubranges (negatives, to: </span><span style="color: #800080;">0</span><span style="color: #000000;">) </span><span style="color: #008000;">//</span><span style="color: #008000;"> numbers == [-5, -3, -9, 10, 12, 14, 15]</span> |
使用独立软件包
Standard Library Preview 包正在持续开发中,并且维护的主要版本0
以指示它并不旨在稳定源。如果您需要项目中的源代码稳定性,则每个批准的标准库建议书也可以独立包装提供。预览包实际上是一个伞形库,可重新导出每个单独的包。
预览版标准库正在持续开发中,如果你的项目对于代码稳定性有要求,每个已通过的标准库提案能够以独立软件包的形式提供。例如,SE-0270 提案的功能可通过独立的 SE0270_RangeSet 软件包获得。因此,如果只需要使用 SE-0270,则可以将其作为软件包依赖项和目标依赖项添加到Package.swift
文件中:
1 2 3 4 5 6 7 8 9 10 11 | let package =<span style="color: #000000;"> Package ( name: </span><span style="color: #800000;">"</span><span style="color: #800000;">MyPackage</span><span style="color: #800000;">"</span><span style="color: #000000;">, dependencies: [ .package (url: </span><span style="color: #800000;">"</span><span style="color: #800000;">https://github.com/apple/swift-se0270-range-set.git</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">from</span>: <span style="color: #800000;">"</span><span style="color: #800000;">1.0.0</span><span style="color: #800000;">"</span><span style="color: #000000;">), ], targets: [ .target ( name: </span><span style="color: #800000;">"</span><span style="color: #800000;">MyTarget</span><span style="color: #800000;">"</span><span style="color: #000000;">, dependencies: [.product (name: </span><span style="color: #800000;">"</span><span style="color: #800000;">SE0270_RangeSet</span><span style="color: #800000;">"</span>, package: <span style="color: #800000;">"</span><span style="color: #800000;">swift-se0270-range-set</span><span style="color: #800000;">"</span><span style="color: #000000;">)]), ] )</span> |
然后导入 SE-0270:
1 2 3 4 5 6 7 8 | <span style="color: #000000;">import SE0270_RangeSet </span><span style="color: #0000ff;">var</span> numbers = [<span style="color: #800080;">10</span>, <span style="color: #800080;">12</span>, -<span style="color: #800080;">5</span>, <span style="color: #800080;">14</span>, -<span style="color: #800080;">3</span>, -<span style="color: #800080;">9</span>, <span style="color: #800080;">15</span><span style="color: #000000;">] let negatives </span>= numbers.subranges (<span style="color: #0000ff;">where</span>: { $<span style="color: #800080;">0</span> < <span style="color: #800080;">0</span><span style="color: #000000;"> }) </span><span style="color: #008000;">//</span><span style="color: #008000;"> numbers[negatives].count == 3</span> <span style="color: #000000;"> numbers.moveSubranges (negatives, to: </span><span style="color: #800080;">0</span><span style="color: #000000;">) </span><span style="color: #008000;">//</span><span style="color: #008000;"> numbers == [-5, -3, -9, 10, 12, 14, 15]</span> |
1 |
1 |
来自:
开源中国社区