ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

 

์ตœ๊ทผ, ์•„ํ‚คํ…์ฒ˜ ๊ด€๋ จ ๊ณต๋ถ€๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ Input/OutputํŒจํ„ด์„ ๊ณต๋ถ€ํ•ด ๋ณธ ๊ฒฝํ—˜์ด ์žˆ์Šต๋‹ˆ๋‹ค..!

์ด๋Ÿฌํ•œ Input/Output ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ๋•Œ, ์ •ํ˜•ํ™” ๋˜๊ณ  ๋ฐ˜๋ณต๋œ ์ฝ”๋“œ๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด

ViewModelProtocol๋ฅผ ์„ ์–ธํ•˜์—ฌ ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

๋ณดํŽธ์ ์œผ๋กœ, ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŽ์ด ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค

protocol ViewModelType {
    
    associatedtype Input
    associatedtype Output
    
    func transform(input: Input) -> Output
    
}

(๋ฌผ๋ก , MVVMํŒจํ„ด๊ณผ RxSwift๋ฅผ ํ˜ผ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•„ ํ•ด๋‹น ํ”„๋กœํ† ์ฝœ์— disposeBag์„ ์„ ์–ธํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค)

 

์ด๋•Œ, Input๊ณผ Output ์•ž์— ๋ถ™์–ด์žˆ๋Š” associatedtype์ด ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

 


 

associatedtype ์ด๋ž€?

์šฐ์„  ์ด associatedtype์€ ํ”„๋กœํ† ์ฝœ์— ์‚ฌ์šฉ์ด ๋˜๋Š” ํ‚ค์›Œ๋“œ์ž…๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š”, 'ํŠน์ • ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€๋ฅผ ๋งŒ๋“ค์–ด ๋†“์ง€ ์•Š๊ณ , ์ฒญ์‚ฌ์ง„๋งŒ์„ ์ œ์‹œํ•˜๊ธฐ ์œ„ํ•จ' ์ด๋ผ๊ณ  ์•Œ๊ณ ์žˆ์ฃ ?

์ด๋Ÿฌํ•œ ๋ชฉ์ ๊ณผ associatedtype์˜ ์—ญํ• ์€ ๊ฑฐ์˜ ์ผ๋งฅ์ƒํ†ตํ•˜๋‹ค๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค!

 

์šฐ๋ฆฌ๊ฐ€ ๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค๊ณ  ๊ฐ€์ • ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

protocol StudentProtocol{
    var className: Int { get }
    var name: String { get }
    var age: Int { get }
}

์ด StudentProtocol์„ ์ฑ„ํƒํ•˜๋Š” ๊ณณ์—์„œ๋Š” ๋ฐ˜๋“œ์‹œ className, name, age๋ฅผ ์ •์˜ ํ•ด ์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ, className์ด "ํ•ด๋ฐ”๋ผ๊ธฐ๋ฐ˜" ๊ณผ ๊ฐ™์ด Stringํƒ€์ž…์˜ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ๋„ ์žˆ์ง€ ์•Š์„๊นŒ์š”??

์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ ์šฐ๋ฆฌ๋Š” associatedtype์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!!

 

protocol StudentProtocol{

    associatedtype WhatName 
    
    var className: WhatName { get }
    var name: String { get }
    var age: Int { get }
}

์ด๋Ÿฐ ์‹์œผ๋กœ,  ํ”„๋กœํ† ์ฝœ์— ์ •์˜๋˜์–ด ์žˆ๋Š” ํƒ€์ž…์ด ์‹ค์ œ ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•  ๋•Œ ๊ฒฐ์ •๋˜๊ฒŒ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š”

ํ‚ค์›Œ๋“œ๊ฐ€ ๋ฐ”๋กœ associatedtype์ž…๋‹ˆ๋‹ค.

 

๋น„์œ ํ•˜์ž๋ฉด, Type์˜ Placeholder์™€ ๊ฐ™์€ ์—ญํ• ์ด๋ผ๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!!

 

๋”ฐ๋ผ์„œ, ์‹ค์ œ StudentProtocol์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ณณ์—์„œ className์˜ return์„ Int, String ๋“ฑ์œผ๋กœ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

'๐ŸŽ > Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Swift] @discardableResult ๋ž€?  (0) 2023.03.06
[Swift] final ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š”??  (0) 2023.02.08
[Swift] lazy ํ‚ค์›Œ๋“œ๋ž€?  (2) 2023.01.30
๋Œ“๊ธ€
๋ฐ˜์‘ํ˜•
๋งํฌ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
Total
Today
Yesterday