๐Ÿฆ… Swift

Methods

Kiwi๐Ÿ’ป 2022. 7. 4. 17:16

ํŠน์ • ํƒ€์ž…์˜ ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•๊ณผ ๊ด€๋ จ๋œ ํ•จ์ˆ˜๋ฅผ ๋ฉ”์†Œ๋“œ๋ผ ํ•œ๋‹ค. ํŠน์ • ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ, ํŠน์ • ํƒ€์ž…๊ณผ ๊ด€๋ จ๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ํƒ€์ž… ๋ฉ”์†Œ๋“œ๋ผ ํ•œ๋‹ค.Swift์—์„œ๋Š” ํด๋ž˜์Šค ํƒ€์ž… ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•์—์„œ๋„ ๋ฉ”์†Œ๋“œ๋ฅผ ์„ ์–ธํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ(Instance Methods)

์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•์˜ ์ธ์Šคํ„ด์Šค์— ์†ํ•œ ๋ฉ”์†Œ๋“œ์ด๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค ๋‚ด์˜ ๊ฐ’์„ ์ œ์–ดํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋Š” ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ๊ทธ ์ธ์Šคํ„ด์Šค๊ฐ€ ์†ํ•œ ํŠน์ • ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค์—์„œ๋งŒ ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค.

class Counter {
    var count = 0
    func increment() {
        count += 1
    }
    func increment(by amount: Int) {
        count += amount
    }
    func reset() {
        count = 0
    }
}

self ํ”„๋กœํผํ‹ฐ(The self Property)

๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋Š” ์•”์‹œ์ ์œผ๋กœ ์ธ์Šคํ„ด์Šค ์ž์ฒด๋ฅผ ์˜๋ฏธํ•˜๋Š” self๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š”๋‹ค. ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ selfํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•ด ์ธ์Šคํ„ด์Šค ์ž์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ธ์ž ์ด๋ฆ„์ด ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ๋ช…์‹œ์ ์œผ๋กœ selfํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ์€ selfํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ํ”„๋กœํผํ‹ฐ์˜ ๋ชจํ˜ธํ•จ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•œ ์˜ˆ๋‹ค.

struct Point {
    var x = 0.0, y = 0.0
    func isToTheRightOf(x: Double) -> Bool {
        return self.x > x  // self.x๋ฅผ ์ด์šฉํ•ด ํ”„๋กœํผํ‹ฐ x์™€ ์ธ์ž x๋ฅผ ๊ตฌ๋ถ„
    }
}
let somePoint = Point(x: 4.0, y: 5.0)
if somePoint.isToTheRightOf(x: 1.0) {
    print("This point is to the right of the line where x == 1.0")
}
// "This point is to the right of the line where x == 1.0" ์ถœ๋ ฅ

์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ ๋‚ด์—์„œ ๊ฐ’ ํƒ€์ž… ๋ณ€๊ฒฝ (Modifying Value Types from Within Instance Methods)

๊ตฌ์กฐ์ฒด์™€ ์—ด๊ฑฐํ˜•์€ ๊ฐ’ ํƒ€์ž…์ด๋‹ค. ๊ทธ๋ž˜์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ ๋‚ด์—์„œ ๊ฐ’ ํƒ€์ž…์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ํ•˜์ง€๋งŒ ๊ฐ’ ํƒ€์ž… ํ˜•์˜ ๋ฉ”์†Œ๋“œ์—์„œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ๋ฉ”์†Œ๋“œ์— mutating๋ถ™์—ฌ ์ฃผ๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค. mutating์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™์€ ๋ฉ”์†Œ๋“œ์—์„œ๋Š” ๋ฉ”์†Œ๋“œ์˜ ๊ณ„์‚ฐ์ด ๋๋‚œ ํ›„ ์›๋ณธ ๊ตฌ์กฐ์ฒด์— ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฎ์–ด ์จ์„œ ๊ทธ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

struct Point {
    var x = 0.0, y = 0.0
    mutating func moveBy(x deltaX: Double, y deltaY: Double) {
        x += deltaX
        y += deltaY
    }
}
var somePoint = Point(x: 1.0, y: 1.0)
somePoint.moveBy(x: 2.0, y: 3.0)
print("The point is now at (\(somePoint.x), \(somePoint.y))")
// "The point is now at (3.0, 4.0)" ์ถœ๋ ฅ

ํƒ€์ž… ๋ฉ”์†Œ๋“œ (Type Methods)

์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค์—์„œ ํ˜ธ์ถœ๋˜๊ณ , ํƒ€์ž… ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ํƒ€์ž… ์ž์ฒด์—์„œ ํ˜ธ์ถœํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ํƒ€์ž… ๋ฉ”์†Œ๋“œ์˜ ์„ ์–ธ์€ ๋ฉ”์†Œ๋“œ ํ‚ค์›Œ๋“œ func์•ž์— staticํ˜น์€ classํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค. static๋ฉ”์†Œ๋“œ์™€ class๋ฉ”์†Œ๋“œ์˜ ์ฐจ์ด์ ์€ static๋ฉ”์†Œ๋“œ๋Š” ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ์ˆ˜ ์—†๋Š” ํƒ€์ž… ๋ฉ”์†Œ๋“œ ์ด๊ณ , class๋ฉ”์†Œ๋“œ๋Š” ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž… ๋ฉ”์†Œ๋“œ ๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

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

Inheritance  (0) 2022.07.05
Subscripts  (0) 2022.07.04
Properties  (0) 2022.07.04
Structures and Classes  (0) 2022.06.22
Enumerations  (0) 2022.06.15