본문 바로가기

프로그래밍 언어/Swift

[Swift] 튜플 tuple

반응형
튜플이란?

튜플은 하나의 변수에 여러개의 값을 담을 수 있으므로 바구니 형태이며, 인덱스키 값을 가집니다. 

 

이번 포스팅에서는 지난번 배열딕셔너리 , 에 이어 튜플에 대해 알아보도록 하겠습니다. 

 

1. 튜플 만들기 

var data:(String, Int, Double) = ("Apple", 100, 3.14)
print(data.0, data.1, data.2)
Apple 100 3.14

 

2. 값 변경 

data.0 = "Kiwi"
data.1 = 999
print(data)
("Kiwi", 999, 3.14)

 

3. 키 값 지정하기

var test:[String:Int] = ["a":10, "b":7, "c":99]

for item in test {
    print(item)
    print(item.0, item.key)
    print(item.1, item.value)
}
(key: "b", value: 7)
b b
7 7
(key: "a", value: 10)
a a
10 10
(key: "c", value: 99)
c c
99 99 

 

4. 타입 지정

typealias FirstTuple = (name:String, score:Int, point:Double)
var data2: FirstTuple = ("Apple", 100, 3.14)

print(data2)
(name: "Apple", score: 100, point: 3.14)

 

반응형