ExpressibleByArrayLiteral
声明
protocol ExpressibleByArrayLiteral
概述
数组字面量是表示值列表的一种简单方式。只需用方括号将值、实例或文字的逗号分隔列表括起来即可创建数组文字。您可以在任何需要 ExpressibleByArrayLiteral
类型的实例的地方使用数组字面量:作为分配给变量或常量的值,作为方法或初始化程序的参数,甚至作为非变异操作(如 map(_:)
)的主题或 filter(_:)
。
数组、集合和选项集都符合 ExpressibleByArrayLiteral
,您自己的自定义类型也可以。下面是一个使用数组字面量创建集合和数组的示例:
let employeesSet: Set<String> = ["Amir", "Jihye", "Dave", "Alessia", "Dave"]
print(employeesSet)
// Prints "["Amir", "Dave", "Jihye", "Alessia"]"
let employeesArray: [String] = ["Amir", "Jihye", "Dave", "Alessia", "Dave"]
print(employeesArray)
// Prints "["Amir", "Jihye", "Dave", "Alessia", "Dave"]"
Set
和 Array
类型各自以自己的方式处理数组文字来创建新实例。在这种情况下,新创建的集合删除了重复值 (“Dave”),并且不保持数组字面量元素的顺序。另一方面,新数组与提供的元素的顺序和数量相匹配。
数组文字的类型推断
只要有可能,Swift 的编译器就会推断出你的数组字面量的完整预期类型。因为Array
是数组文字的默认类型,所以无需编写任何其他代码,您可以通过提供一个或多个值来声明具有特定元素类型的数组。
在这个例子中,编译器推断出每个数组字面量的完整类型。
let integers = [1, 2, 3]
// 'integers' has type '[Int]'
let strings = ["a", "b", "c"]
// 'strings' has type '[String]'
单独的空数组文字不能为编译器提供足够的信息来推断 Array
实例的预期类型。使用空数组文字时,请指定变量或常量的类型。
var emptyArray: [Bool] = []
// 'emptyArray' has type '[Bool]'
因为许多函数和初始化程序完全指定了它们的参数类型,所以您通常可以使用带有或不带有元素的数组字面量作为参数。例如,此处显示的 sum(_:)
函数将 Int
数组作为参数:
func sum(values: [Int]) -> Int {
return values.reduce(0, +)
}
let sumOfFour = sum([5, 10, 15, 20])
// 'sumOfFour' == 50
let sumOfNone = sum([])
// 'sumOfNone' == 0
当您调用未完全指定其参数类型的函数时,请使用 type-cast 运算符 (as
) 指定数组字面量的类型。例如,此处显示的 log(name:value:)
函数具有不受约束的通用 value
参数。
func log<T>(name name: String, value: T) {
print("\(name): \(value)")
}
log(name: "Four integers", value: [5, 10, 15, 20])
// Prints "Four integers: [5, 10, 15, 20]"
log(name: "Zero integers", value: [] as [Int])
// Prints "Zero integers: []"
符合ExpressibleByArrayLiteral
通过声明 init(arrayLiteral:)
初始化程序,将使用数组文字初始化的函数添加到您自己的自定义类型。以下示例显示了假设的 OrderedSet
类型的数组文字初始化程序,该类型具有类似集合的语义,但保持其元素的顺序。
struct OrderedSet<Element: Hashable>: Collection, SetAlgebra {
// implementation details
}
extension OrderedSet: ExpressibleByArrayLiteral {
init(arrayLiteral: Element...) {
self.init()
for element in arrayLiteral {
self.append(element)
}
}
}
可用版本
相关用法
- Swift ExpressibleByArrayLiteral init(arrayLiteral:)用法及代码示例
- Swift ExpressibleByIntegerLiteral用法及代码示例
- Swift ExpressibleByStringInterpolation init(stringInterpolation:)用法及代码示例
- Swift ExpressibleByUnicodeScalarLiteral用法及代码示例
- Swift ExpressibleByFloatLiteral init(floatLiteral:)用法及代码示例
- Swift ExpressibleByIntegerLiteral init(integerLiteral:)用法及代码示例
- Swift ExpressibleByExtendedGraphemeClusterLiteral用法及代码示例
- Swift ExpressibleByStringLiteral用法及代码示例
- Swift ExpressibleByFloatLiteral用法及代码示例
- Swift ExpressibleByStringInterpolation用法及代码示例
- Swift ExpressibleByDictionaryLiteral用法及代码示例
- Swift ExpressibleByBooleanLiteral init(booleanLiteral:)用法及代码示例
- Swift EnumeratedSequence forEach(_:)用法及代码示例
- Swift EnumeratedSequence.Iterator map(_:)用法及代码示例
- Swift EnumeratedSequence.Iterator flatMap(_:)用法及代码示例
- Swift EmptyCollection first(where:)用法及代码示例
- Swift EmptyCollection index(_:offsetBy:limitedBy:)用法及代码示例
- Swift EnumeratedSequence drop(while:)用法及代码示例
- Swift EmptyCollection partition(by:)用法及代码示例
- Swift EnumeratedSequence.Iterator allSatisfy(_:)用法及代码示例
- Swift EmptyCollection prefix(upTo:)用法及代码示例
- Swift EmptyCollection starts(with:)用法及代码示例
- Swift EnumeratedSequence用法及代码示例
- Swift EmptyCollection.Iterator split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代码示例
- Swift EmptyCollection.Iterator starts(with:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 ExpressibleByArrayLiteral。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。