본문 바로가기
Programming/Swift

[Swift] Tuple

by 생각하는달팽이 2016. 2. 10.

이번에는 튜플에 대해서 공부하였습니다.

튜플은 기존의 NS 시스템에서는 볼 수 없었던 특이한 점입니다.

아래의 예를 보시면요.


unamedTuple 의 경우 키값이 없는 튜플이고.

var unamedTuple = (123,455)


namedTuple 의 경우에는 키값이 존재하는 튜플입니다.


var namedTuple = (first:123,second:455)


튜플은 쉽게 생각하면 딕셔너리갖다고 생각하시면 될것 같습니다. 


다만, 다르게 느껴지는 점이라면요.

딕셔너리에서는 키값을 인덱스로 가져다 쓴다면.

이녀석은 자바스크립트에서 정의한 배열의 느낌이 강합니다.

class 같은 느낌이 좀 더 강합니다.


// 2016. 02. 10. Ruke

// TUPLES


var products = [123: "Bananas",234:"Apples",345:"Pears",456:"Oranges"]


for(sku,product) in products{

    print("The sku for \(product) is \(sku)")

}



var mac = (model : "Macbook Pro", year:2015)


mac.model


mac.year


func getCarDetails() -> (model: String, topSpeed: Int, isConvertable:Bool) {

    var model = "Camaro"

    var topSpeed = 145

    var isConvertable = true

    

    return (model,topSpeed,isConvertable)

}


var camaro = getCarDetails();


camaro.model


camaro.topSpeed


camaro.isConvertable

반응형

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

[Swift] Enumerations  (0) 2016.02.10
[Swift] Optional  (0) 2016.02.10
[Swift] Set  (0) 2016.02.10
[Swift] Dictionary  (0) 2016.02.10
[Swift] Array  (0) 2016.02.10