[swift] 42. 콜렉션 합치기

RudinP·2023년 10월 6일
0

Study

목록 보기
61/227

콜렉션 합치기

  • list [ ], set<>, dictionary[:]
  • 콜렉션은 +를 통해 합칠 수 있다.
let myFriends = ["철수", "영희", "수잔"]
let otherFriends = ["제임스", "존슨", "존시나"]

let totalFriends = myFriends + otherFriends
  • .append(contentsOf:)를 사용하려면 var이어야 한다.
myFriends.append(contentsOf: otherFriends) //myFriends let 불가
  • 같은 콜렉션이기 때문에 list와 set을 합칠 수 있다.
    • 다만 set에서 list로 형변환이 발생한다.
let myFriends = ["철수", "영희", "수잔"]
let otherFriends: Set<String> = ["제임스", "존슨", "존시나"]

let totalFriends = myFriends + otherFriends
totalFriends //[String]
profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글