现在已合并到 unstandard 库中。
一系列便捷的结果构建器。
大多数构建器类型不需要 Foundation。需要 Swift 5.4+。
如果所有元素都为 nil,则返回 true
。元素不需要是同质的。
struct Element {
var title: String?
var description: String?
var child: Self?
@AllNil var isEmpty: Bool {
title
description
child
}
}
true
布尔值的总数
struct FooBar {
var foo: Int?
var bar: Bool?
@Count var nonOptionalCount: Int {
foo != nil
bar != nil
}
}
在构建 Set 属性时,避免一些标点符号。
struct FooBar {
var foo: Int
var bar: Int
@SetResult var fooAndBar: Set<Int> {
foo
bar
}
}
返回单个元素。有助于避免键入几个 return
关键字。
extension Bool {
@SingleResult var enEspañol: String {
switch self {
case true: "Sí"
case false: "No""
}
}
}
用于指定 UUID 属性的便捷格式化程序。(需要 Foundation)
protocol UUIDIdentifiable: Identifiable where ID == UUID {
@UUIDResult var id: UUID { get }
}
struct Element: UUIDIdentifiable {
var id: UUID {
(194, 210, 39, 207, 170, 13, 68, 151, 190, 189, 41, 237, 240, 95, 174, 248)
}
}
用于通过字符串指定 URL 的便捷格式化程序,适用于您知道有效的 URL。URL 字符串被不安全地展开。(需要 Foundation)
protocol Website {
@URLResult var url: URL
}
struct Wikipedia: Website {
var url: URL {
"https://wikipedia.org"
}
}