Swift Collections Set

Set

Set은 정의된 순서 없이 collection에 동일한 Type의 고유한 값을 저장한다. 항목의 순서가 중요하지 않거나 항목이 한 번만 표시되도록 해야 하는 경우 array 대신 Set을 사용할 수 있다.

NOTE: Swift’s Set type is bridged to Foundation’s NSSet class.

For more information about using Set with Foundation and Cocoa, see Bridging Between Set and NSSet.

Value type 이다.

@frozen public struct Set<Element> where Element : Hashable

Hash Values for Set Types

A type must be hashable in order to be stored in a set—that is, the type must provide a way to compute a hash value for itself. A hash value is an Int value that’s the same for all objects that compare equally, such that if a == b, the hash value of a is equal to the hash value of b.

All of Swift’s basic types (such as String, Int, Double, and Bool) are hashable by default, and can be used as set value types or dictionary key types. Enumeration case values without associated values (as described in Enumerations) are also hashable by default.

Set Type Syntax

The type of a Swift set is written as Set, where Element is the type that the set is allowed to store. Unlike arrays, sets don’t have an equivalent shorthand form.

Creating and Initializing an Empty Set

Image

Image

subscript(_:)

O(1)

count

O(1)

contains(_:)

O(1)

contains(where:)

O(1)

removeFirst()

O(1)

firstIndex(of:)

O(1)

출처

github.com/apple/swift/blob/main/stdlib/public/core/Set
swift.org CollectionTypes

댓글남기기