본문 바로가기

iOS

[Swift] Collection - Set

Set

Collection 중 하나인 set은 다른 Collection type들처럼 Set<타입>을 통해 선언할 수 있다.

 

 

insert를 이용해서 set에 추가할 수 있고 remove 삭제 할 수 도 있다.

removeAll()로 모든 요소 삭제도 가능하다.

 

 

for문에서 sorted를 사용해서 정렬된 값을 받을 수 도 있다.

 

이 외에도 다른 Collection type들 처럼  isEmpty, count, contains 등등 다양한 기능 사용가능하다.

 

intersection() : 교집합    ex) a.intersection(b)

symmetricDifference() : 대칭차집합

union() : 합집합

subtracting() : 차집합

 

'iOS' 카테고리의 다른 글

[swift] Struct and Class  (0) 2023.07.31
[Swift] Closure  (0) 2023.07.29
[Swift] Collection - Dictionary  (0) 2023.07.27
[Swift] Collection - Array  (0) 2023.07.26
[Swift] optional  (0) 2023.07.25