当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Swift Set init(arrayLiteral:)用法及代码示例


用法一

初始化器

init(arrayLiteral:)

创建一个包含给定数组字面量元素的集合。

声明

init(arrayLiteral elements: Element...)
Element 符合 Hashable 时可用。

参数

elements

新集合的元素的可变参数列表。

详述

不要直接调用此初始化程序。当您使用数组文字时,编译器会使用它。相反,通过将逗号分隔的值列表括在方括号中,使用数组文字作为其值来创建一个新集合。您可以在类型上下文预期集合的任何地方使用数组文字。

在这里,一组字符串是从仅包含字符串的数组文字创建的。


let ingredients: Set = ["cocoa beans", "sugar", "cocoa butter", "salt"]
if ingredients.isSuperset(of: ["sugar", "salt"]) {
    print("Whatever it is, it's bound to be delicious!")
}
// Prints "Whatever it is, it's bound to be delicious!"

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

用法二

初始化器

init(arrayLiteral:)

创建一个包含给定数组字面量元素的集合。

声明

init(arrayLiteral: Self.Element...)
ArrayLiteralElementSelf.Element 时可用。

参数

arrayLiteral

新集合的元素列表。

详述

不要直接调用此初始化程序。当您使用数组文字时,编译器会使用它。相反,通过将逗号分隔的值列表括在方括号中,使用数组文字作为其值来创建一个新集合。您可以在类型上下文预期集合的任何地方使用数组文字。

在这里,一组字符串是从仅包含字符串的数组文字创建的:


let ingredients: Set = ["cocoa beans", "sugar", "cocoa butter", "salt"]
if ingredients.isSuperset(of: ["sugar", "salt"]) {
    print("Whatever it is, it's bound to be delicious!")
}
// Prints "Whatever it is, it's bound to be delicious!"

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

相关用法


注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Set init(arrayLiteral:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。