ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Swift] ํƒ€์ž…์บ์ŠคํŒ…(type casting)
    iOS/Swift 2022. 8. 24. 23:53

     


    ๐Ÿ’ก ์Šค์œ„ํ”„ํŠธ์˜ ํƒ€์ž… ์บ์ŠคํŒ…์€ C++๊ณผ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฑธ ์ดํ•ดํ•˜๊ณ  ๋„˜์–ด๊ฐ€์ž!!!

    ์ด๊ฑธ ๋ชจ๋ฅด๊ณ  ๊ณต๋ถ€ํ•˜๋ฉด ํ—ท๊ฐˆ๋ฆฐ๋‹ค

     

     

    C++์˜ ํ˜•๋ณ€ํ™˜(ํƒ€์ž… ์บ์ŠคํŒ…)

    - ๋ฌต์‹œ์  ํ˜•๋ณ€ํ™˜

    ์ปดํŒŒ์ผ ํ•  ๋•Œ ์ž๋™์œผ๋กœ ํ˜•๋ณ€ํ™˜ ๋œ๋‹ค.

    float a = 3.14;
    int b;
    
    b = a;   // 3

     

    - ๋ช…์‹œ์  ํ˜•๋ณ€ํ™˜

    ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ค€๋‹ค.

    float a = 3.14;
    int b;
    
    b = (int)a;   // 3

     

    Swift์˜ ํ˜•๋ณ€ํ™˜(์ด๋‹ˆ์…œ๋ผ์ด์ €)

    var a: Double = 3.14
    var b: Int = Int(a)
    
    print(b)   // 3

     

    Swift๋Š” ๊ธฐ์กด ๊ฐ’(a)์„ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›๋Š” ์ด๋‹ˆ์…”๋ผ์ด์ €๋ฅผ ํ†ตํ•ด ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

     

     


    ๐Ÿ’ก ์Šค์œ„ํ”„ํŠธ์˜ ํƒ€์ž… ์บ์ŠคํŒ…

    1.  ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž… ํ™•์ธ
    2.  ์ž์‹ ์„ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค์ธ์–‘ ํ–‰์„ธํ•˜๊ธฐ

     

    1๏ธโƒฃ ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž… ํ™•์ธ: as

    ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ผ๋ฉด true ๋ฐ˜ํ™˜

    ์•„๋‹ˆ๋ผ๋ฉด false ๋ฐ˜ํ™˜

     

    
    // ๋ถ€๋ชจ ํด๋ž˜์Šค ์ƒ์„ฑ
    class Flower {
        var name: String
        
        init(name: String) {
            self.name = name
        }
        
        var description: String {
            return "This flower is \(name)"
        }
    }
    
    // ์ž์‹ ํด๋ž˜์Šค-1 ์ƒ์„ฑ
    class Freesia:Flower {
        var color: String
        
        override var description: String {
            return "This flower is \(name) and color is \(color)"
        }
        
        init(color: String, name: String){
            self.color = color
            super.init(name: name)
        }
    }
    
    // ์ž์‹ ํด๋ž˜์Šค-2 ์ƒ์„ฑ
    class Rose:Flower {
        var season: String
        
        override var description: String {
            return "This flower is \(name) and It blooms in May-June"
        }
        
        init(season: String, name: String){
            self.season = season
            super.init(name: name)
        }
    }
    
    
    // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    let flower:Flower = Flower(name: "cherryBlossom")
    print(flower.name)
    // cherryBlossom
    
    let myFavor:Freesia = Freesia(color: "yellow", name: "Freesia")
    print(myFavor.description)
    // This flower is Freesia and color is yellow
    
    let yourFavor:Rose = Rose(season: "Spring", name: "Rose")
    print(yourFavor.description)
    // This flower is Rose and It blooms in May-June
    
    // is: ๋ฐ์ดํ„ฐ ํƒ€์ž… ํ™•์ธ
    print(flower is Flower)     //true
    print(flower is Freesia)    //false
    print(flower is Rose)       //false

     

    2๏ธโƒฃ ๋‹ค์šด์บ์ŠคํŒ…

    ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ํƒ€์ž…์„ ์ž์‹ ํด๋ž˜์Šค์˜ ํƒ€์ž…์œผ๋กœ ์บ์ŠคํŒ…ํ•œ๋‹ค

     

    ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ Flower์€ ๋ถ€๋ชจ ํด๋ž˜์Šค์ด๊ณ , Rose๋Š” ์ž์‹ ํด๋ž˜์Šค์ผ ๋•Œ
    Flower -> Rose : ์—…์บ์ŠคํŒ… (ํ•ญ์ƒ ์„ฑ๊ณต)
    Rose -> Flower: ๋‹ค์šด์บ์ŠคํŒ… (์‹คํŒจ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด๋ถ€ ์—ฐ์‚ฐ์ž as?๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค)

     

    // as: ๋‹ค์šด ์บ์ŠคํŒ…
    if let givingFlower: Freesia = flower as? Freesia {
        print("This is yours")
    } else {
        print(flower.description)
    }
    // ๊ฒฐ๊ณผ : This flower is cherryBlossom
    
    if let givingFlower: Rose = yourFavor as? Rose {
        print("This is yours")
    } else {
        print(yourFavor.description)
    }
    // ๊ฒฐ๊ณผ : This is yours
    // yourFavor์ด ์ฐธ์กฐํ•˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ Rose ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๋ผ๋ฉด givingFlower์ด๋ผ๋Š” ์ž„์‹œ ์ƒ์ˆ˜์— ํ• ๋‹นํ•˜๋ผ๋Š” ๋œป

     

     

    ์ฐธ๊ณ )

     

     

    ํƒ€์ž…์บ์ŠคํŒ… (Type Casting) - The Swift Language Guide (ํ•œ๊ตญ์–ด)

    library๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” Movie,Song์ธ์Šคํ„ด์Šค์˜ ๊ณตํ†ต ๋ถ€๋ชจ๋Š” MediaItem์ด๊ธฐ ๋•Œ๋ฌธ์— library๋Š” ํƒ€์ž… ์ถ”๋ก ์— ์˜ํ•ด [MediaItem] ๋ฐฐ์—ด์˜ ํ˜•์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. library๋ฅผ ์ˆœํšŒ(iterate)ํ•˜๋ฉด ๋ฐฐ์—ด์˜ ์•„์ดํ…œ์€ Movie, Song ํƒ€์ž…์ด

    jusung.gitbook.io

     

     

     

Designed by Tistory.