Focus On Develop ๐ŸคŸ๐ŸคŸ

[Swift] AssociatedType ๋ณธ๋ฌธ

iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!

[Swift] AssociatedType

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2021. 8. 23. 10:19

 

์•ˆ๋…•ํ•˜์„ธ์š” ๐Ÿ‘‹ ์˜ค๋Š˜์€ AssociatedType์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ๊ฑฐ์—์š”!

๋ง ๊ทธ๋Œ€๋กœ ๊ด€๋ จ๋œ ํƒ€์ž…..? ์—ฐ๊ด€๋œ ํƒ€์ž…..? ๊ทธ๋Ÿฐ๊ฑด๋ฐ, ์‚ฌ์‹ค์€ Generic์—์„œ placeholder ํƒ€์ž…๊ฐ™์€๊ฑฐ์—์š”!!

๋ฌด์Šจ๋ง์ด๋ƒ.. ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด์„œ ๋ด…์‹œ๋‹ค์š”!!

 


protocol Appendable {
    var collection: [String] { get set }
    func append(_ item: String)
}

class StringArray: Appendable {
    var collection: [String] = []
    
    func append(_ item: String) {
        collection.append(item)
    }
}

Appendable์ด๋ผ๋Š” ์–ด๋–ค Array์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ protocol์ด ์žˆ์–ด์š”.

์ด Appendable์ด๋ผ๋Š” ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๋Š” StringArray๋ผ๋Š” classํƒ€์ž…์—์„œ, ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•œ๋‹ต์‹œ๊ณ  ๋ฉ”์†Œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•ด์ฃผ๋Š”๋ฐ์š”!

์šฐ๋ฆฌ๊ฐ€ ํ”„๋กœํ† ์ฝœ์„ ์“ฐ๋ฉด์„œ ์›ํ•˜๋Š”๊ฑด ์ด๊ฒŒ ์•„๋‹ˆ๊ฒ ์ฃ ~?

 

class IntArray: Appendable {
    var collection: [Int] = []
    
    func append(_ item: Int) {
        collection.append(item)
    }
}

์ด๋Ÿฐ๊ฒƒ๋„ ๊ฐ™์ด ์“ฐ๊ณ  ์‹ถ์ž–์•„์š”~~~ ๐Ÿ˜‚

๊ทผ๋ฐ ์ง€๊ธˆ ์ƒํƒœ๋กœ๋ฉด IntArray๋Š” ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ• ๊ฑฐ์—์š”. Intํƒ€์ž…์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด Appendable ํ”„๋กœํ† ์ฝœ์—๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.

 

์ด๋•Œ!! ์“ธ ์ˆ˜ ์žˆ๋Š”๊ฒŒ ๋ฐ”๋กœ associatedType ์ด์—์š”.

protocol Appendable {
    associatedtype SomeType
    var collection: [SomeType] { get set }
    func append(_ item: SomeType)
}

์ฒ˜์Œ์— associatedtype์„ ์†Œ๊ฐœํ• ๋•Œ ์–ด๋–ค Generic์˜ Placeholder๊ฐ™์€๊ฑฐ๋ผ๊ณ  ํ–ˆ์ฃ ~? ์ด๋ ‡๊ฒŒ์š”!!

"์–ด๋–ค ํƒ€์ž…์ด๋“  ์ƒ๊ด€์—†์–ด~ ์‚ฌ์šฉํ• ๋•Œ๋งŒ ๋ช…์‹œํ•ด์ฃผ๋ฉด ๋ผ" 

class StringArray: Appendable {
    var collection: [String] = []
    
    func append(_ item: String) {
        collection.append(item)
    }
}

class IntArray: Appendable {
    var collection: [Int] = []
    
    func append(_ item: Int) {
        collection.append(item)
    }
}

๊ทธ๋Ÿฌ๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์›ํ–ˆ๋˜ ์ด๋Ÿฐ๊ฒŒ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋˜์š”!!

๊ทผ๋ฐ ํ”„๋กœํ† ์ฝœ์— ์ •์˜ํ•ด์•ผ ํ•  ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋งŽ์œผ๋ฉด.. ๊ทธ๋•Œ ํ•˜๋‚˜ํ•˜๋‚˜ Int, String ์ณ์ค„๊ฑด๊ฐ€์š”~~? 

์šฐ๋ฆฌ๋Š” ์ž๋™์™„์„ฑ์„ ๋˜ ์‚ฌ์šฉํ•ด์•ผ ํ•˜์ž–์•„์š”! ๐Ÿง‘‍๐Ÿ’ป

 

class StringArray: Appendable {
    typealias SomeType = String
    
    var collection: [String]
    
    func append(_ item: String) {
        <#code#>
    }
}

๋จผ์ € ํ•˜๋‚˜ํ•˜๋‚˜ ์ •์˜ํ•˜๊ธฐ ์ „์—, typealias๋ฅผ ํ†ตํ•ด placeholder๋กœ ์–ด๋–ค ํƒ€์ž…์„ ์“ธ๊ฑด์ง€๋ฅผ ์ •์˜ํ•ด๋ฒ„๋ฆฌ๋Š”๊ฑฐ์—์š”!

๊ทธ๋Ÿฌ๋ฉด ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๊ธฐ ์œ„ํ•ด ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ• ๋•Œ, ์ด๋ ‡๊ฒŒ ์ •์˜ํ•œ ํƒ€์ž…๋Œ€๋กœ ์ž๋™์™„์„ฑ์ด ๋œ๋‹ต๋‹ˆ๋‹ค~

 

associatedtype์€ ์ด๋ ‡๊ฒŒ ์‹ค์ œ ์‚ฌ์šฉํ•  ํƒ€์ž…์„ ์ •์˜ํ•˜๊ธฐ ์ „๊นŒ์ง€ ์ง€์ •๋˜์ง€ ์•Š์œผ๋‹ˆ ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์นœ๊ตฌ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜๋„ ๋ˆ„๊ตฐ๊ฐ€์—๊ฒŒ ๋„์›€์ด ๋˜์—ˆ๊ธธ ๋ฐ”๋ž˜์š” ๐Ÿ™

Comments