새소식

iOS/Swift

[Swift] 공식문서 파헤치기 (Array, Set, Dictionary)

  • -

Array (Struct)

순서가 지정된 무작위 액세스 collection

 

Array | Apple Developer Documentation

An ordered, random-access collection.

developer.apple.com


  • 배열은 앱에서 가장 많이 사용되는 데이터 타입 중 하나

  • 앱의 데이터를 구성할 때 배열을 사용

  • 특히 배열을 사용하여 단일 유형의 element, 즉 배열의 element 유형을 보유

  • 배열은 Int형부터 String, Class까지 모든 종류의 element를 저장 가능

  • 예전 포스팅
 

[Swift] 배열

참조 : Swift 스위프트 프로그래밍 3판 Swift 5, 저자 야곰 (한빛미디어) , The Swift Programming Language Swift 5.6 Edition (iBooks) (https://books.apple.com/kr/book/the-swift-programming-language-swift-5-6/id881256329) 배열(Array) - 같

kimkhuna99.tistory.com


Set (Struct)

고유한 요소의 정렬되지 않은 collection

 

Set | Apple Developer Documentation

An unordered collection of unique elements.

developer.apple.com


  • 멤버십을 효율적으로 테스트해야 하고 collection의 element 순서 에 관심이 없거나
    각 element가 collection에 한 번만 나타나는지 확인해야 하는 경우 Array 대신 Set를 사용

  • Hashable 프로토콜을 준수하는 element Type이면 Set를 생성 가능
    ※ Hashable Protocol : 정수 hash값을 생성하기 위해 Hasher로 해시될 수 있는 유형

  • 기본적으로 표준 라이브러리의 대부분의 유형은 문자열(String), 숫자(Int) 및 Bool, 관련 값(associated value)이 없는 열거형,
    심지어 자체 설정을 포함하여 Hash가 가능

Set의 연산들

  • contains(_:) : Set에 특정 요소가 포함되어 있는가
  • equal to : ==
  • isSubset(of:) : Set에 다른 세트나 스퀸스의 모든 요소가 포함되어 있는가
  • isSuperset(of:) : 모든 요소다른 집합이나 시퀀스에 포함되어 있는가
  • isStrictSubset(of:), isStrictSuperset(of:) : Set가 다른 Set의 하위 Set인가, 상위 Set인가
  • isDisjoint(with:) : 다른 Set와 동일 element가 있는가

두 Set의 결합과 해제 연산들

  • union(_:) : Set의 element와 다른 Set 또는 Sequence로 새로운 Set를 생성할 때 사용
  • intersection(_:) : Set와 다른 Set또는 Sequence에 공통된 요소만으로 새로운 Set를 생성
  • symmertricDifference(_:) : Set나 다른 Set또는 Sequence에 있지만 둘 다에는 없는 element로 새로운 Set를 생성
  • subtracting(_:) : 다른 Set나 Sequence에도 없는 Set요소로 새로운 Set를 생성

Set 수정 연산들

  • formUinion(_:)
  • formIntersection(_:)
  • formSymmertricDifference(_:)
  • subtract(_:)

Dictionary (Struct)

element가 key-value인 collection

 

Dictionary | Apple Developer Documentation

A collection whose elements are key-value pairs.

developer.apple.com


  • Dictionary는 해시 테이블의 한 유형으로 포함된 항목에 대한 빠른 액세스를 제공
    ※ 해시 테이블 : Computing 에서 키를 값에 매칭할 수 있는 구조인 연관 배열 추가에 사용되는 자료 구조

  • 테이블의 각 항목은 String이나 숫자 같은 Hash가 가능한 유형인 를 사용하여 식별

  • 해당 키를 사용하여 임의의 객체일 수 있는 해당 값을 검색

  • 다른 언에서는 유사한 데이터 유형을 해시 또는 연관배열이라고 함

  • 예전 포스팅
 

[Swift] Dictionary 와 Set

참조 : Swift 스위프트 프로그래밍 3판 Swift 5, 저자 야곰 (한빛미디어) , The Swift Programming Language Swift 5.6 Edition (iBooks) (https://books.apple.com/kr/book/the-swift-programming-language-swift-5-6/id881256329) Dictionary - 요소

kimkhuna99.tistory.com

 

 

출처

 

 

해시 테이블 - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전.

ko.wikipedia.org

 

 

Hashable | Apple Developer Documentation

A type that can be hashed into a to produce an integer hash value.

developer.apple.com

 

728x90

'iOS > Swift' 카테고리의 다른 글

[Swift] Dispatch (1)  (0) 2023.11.23
[Swift] RelativeDateTimeFormatter(상대시간)  (0) 2023.11.20
[Swift] UISceneDelegate  (0) 2023.11.09
[Swift] frame vs bounds  (1) 2023.10.28
[Swift] BackgroundTasks  (0) 2023.09.03
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.