๐Ÿ”– ์ฐธ๊ณ 
* [<https://docs.swift.org/swift-book/ReferenceManual/Types.html#grammar_tuple-type-element>](<https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html>)
* [<https://zeddios.tistory.com/238>](<https://zeddios.tistory.com/238>)
* [<https://www.hackingwithswift.com/search/tuple>](<https://www.hackingwithswift.com/search/tuple>)

#tuple

1. tuple?

  1. Objective-C ์—๋Š” ์—†์—ˆ๋˜, Swift์—์„œ ์†Œ๊ฐœ๋œ ์ƒˆ๋กœ์šด ํƒ€์ž….

  2. **(val1, val2, val3)**

  3. ๊ทธ๋ฃน ์•ˆ์˜ ๊ฐ’**member**๋“ค์€ ๋‹ค์–‘ํ•œ ํƒ€์ž…์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ณ , **tuple** ์—ญ์‹œ ๋ฉค๋ฒ„๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

    :named type, compound type, function type

  4. ๊ฐ ๋ฉค๋ฒ„๋“ค์— ์ ‘๊ทผํ•˜๋ ค๋ฉด .Index ๋ฅผ ์ด์šฉํ•œ๋‹ค.

  5. ๊ฐ ๋ฉค๋ฒ„๋“ค์— ๋Œ€ํ•ด์„œ๋„ ์ด๋ฆ„์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

    โ†’ **.element's name** ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

    let someTuple = (22, true, "์ŠคํŠธ๋ง")
    someTuple.0   //22
    someTuple.1   //true
    someTuple.2   //"์ŠคํŠธ๋ง"
    
    let otherTuple = (22, (true, "์ŠคํŠธ๋ง"))
    otherTuple.1   //(.0 true, .1 "์ŠคํŠธ๋ง")
    otherTuple.1.1   //"์ŠคํŠธ๋ง"
    
    func calculateAdd() {}
    func changedMenu() {}
    let functionInTuple = (calculateAdd(), changedMenu())
    
    var userData = (height: 170, weight: 65)
    userData.height   //170
    userData.weight   //65
    
    userData.height = 180
    userData.weight = 60
    userData   //(height 180, weight 60)
    

Untitled

Untitled

  1. Tuple์€ ํ•จ์ˆ˜์˜ return๊ฐ’์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

    (์ผ๋ฐ˜์ ์œผ๋กœ ์ด๋ ‡๊ฒŒ, ํ•จ์ˆ˜๋กœ ๋ถ€ํ„ฐ ๋‹ค์–‘ํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค)

    func userProfile() -> (name: String, hegiht: Int, weight: Int) {
    	return ("yooga", 170, 65)
    }
    
    userProfile()   //(name "yooga", hegiht 170, weight 65)
    

2. Tuple ์ฃผ์˜ ์‚ฌํ•ญ

  1. tuple์— ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ง€์šธ ์ˆ˜ ์—†๋‹ค.

  2. ํ•œ ๋ฒˆ ์„ ์–ธ๋œ tuple ๋ฉค๋ฒ„์˜ **ํƒ€์ž…**์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค.

    var userData = (height: 170, weight: 65)
    userData = (155, 50)
    userData = ("yooga", 50) // ์•ˆ๋œ๋‹ค!
    

3. Tuple๊ณผ typealias