iOS/๐ŸŸ  Swift

[Swift] ํ”„๋กœํ† ์ฝœ ๋ฉ”์„œ๋“œ ์š”๊ตฌ์‚ฌํ•ญ (Method Requirements)

woozzang 2021. 1. 18. 23:03
์ฐธ๊ณ  ์ž๋ฃŒ: Protocols - swift.org

 

์†์„ฑ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ method ๋„ `static` ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด type method๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ผ๋ฐ˜์ ์ธ instance method ์™€ type method์ฒ˜๋Ÿผ ์ •์˜ํ•˜๋ฉด ๋˜์ง€๋งŒ,

ํ”„๋กœํ† ์ฝœ์€ ๊ตฌํ˜„์€ ํ•˜์ง€ ์•Š๋Š” ์š”๊ตฌ์‚ฌํ•ญ ์ด๊ธฐ ๋•Œ๋ฌธ์— { } (curly brace) ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ”๋”” ๋ถ€๋ถ„์€ ์—†์Šต๋‹ˆ๋‹ค.


 

Syntax

protocol SomeProtocol {
    // Type method
    // with no-input, no-return
    static func someTypeMethod()
}

protocol RandomNumberGenerator {
    // Instance Method
    // with no-input, Double type return
    func random() -> Double
}

์œ„์˜ ํ•ด๋‹นํ•˜๋Š” ํ”„๋กœํ† ์ฝœ์„ ์ฑ„์šฉํ•œ ํƒ€์ž…์€ ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์†์„ฑ ์š”๊ตฌ์‚ฌํ•ญ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, static ์œผ๋กœ ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ์—๋Š” class ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์„œ ๊ตฌํ˜„ํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.


 

Mutating Method Requirement

๊ด€๋ จ ๊ธ€ : Mutating Method

 

๊ฐ’ ํƒ€์ž…์— Mutating Method๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Method ์•ž์— `mutating` ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ด๊ฒƒ์€ ๊ฐ’ ํƒ€์ž…์ด ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•  ๊ฒฝ์šฐ๋ฅผ ์—ผ๋‘์— ๋‘” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ํด๋ž˜์Šค๊ฐ€ ์ด ํ”„๋กœํ† ์ฝœ์„ ์ฑ„์šฉํ•˜๋Š” ๊ฒฝ์šฐ mutating  ํ‚ค์›Œ๋“œ๋ฅผ ์ ์„ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

 

 

 

๋!


๐Ÿค–๐ŸŸข[์šฐ์งฑ์˜ iOS ๋ธ”๋กœ๊ทธ]๐Ÿ”ต๐Ÿ’ป

iOS๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋ฐฐ์šด ๋‚ด์šฉ์„ ๊ธฐ๋กํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๋‹ค์Œ ๊ธ€: Initializer Requirements - Protocols

 

Initializer Requirements - Protocols

ํ”„๋กœํ† ์ฝœ์—์„œ initializer ๋Š” method ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ { } (curly brace) ์—†์ด init( ) ๊ณผ ๊ฐ™์€ ๊ผด๋กœ ์„ ์–ธํ•ด์ค๋‹ˆ๋‹ค. protocol SomeProtocol { init(someParameter: Int) } Protocol ์„ ์ฑ„ํƒํ•œ Class์˜ Initializer..

woozzang.tistory.com