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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。