๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โŒจ๏ธ Language/swift

[Swift] ๋ฉ”์„œ๋“œ

by hyebin (Helia) 2024. 3. 21.
๋ฐ˜์‘ํ˜•

๋ฉ”์„œ๋“œ(Methods)

ํŠน์ • ํƒ€์ž…์˜ ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•๊ณผ ๊ด€๋ จ๋œ ํ•จ์ˆ˜

 

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

  • ํŠน์ • ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜•์˜ ์ธ์Šคํ„ด์Šค์— ์†ํ•œ ๋ฉ”์„œ๋“œ
  • ์ธ์Šคํ„ด์Šค ๋‚ด์˜ ๊ฐ’์„ ์ œ์–ดํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
  • ์ธ์Šคํ„ด์Šค๊ฐ€ ์†ํ•œ ํŠน์ • ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค์—์„œ๋งŒ ์‹คํ–‰ ๊ฐ€๋Šฅ
class Counter {
    var count = 0

    func increment() {
        count += 1
    }

    func increment(by amount: Int) {
        count += amount
    }

    func reset() {
        count = 0
    }
}

let counter = Counter()
// ์ดˆ๊ธฐ count ๊ฐ’์€ 0์ž…๋‹ˆ๋‹ค.

counter.increment()
// count ๊ฐ’์ด 1๋กœ ๋ณ€๊ฒฝ ๋์Šต๋‹ˆ๋‹ค.

counter.increment(by: 5)
// count ๊ฐ’์€ ํ˜„์žฌ 6์ž…๋‹ˆ๋‹ค.

counter.reset()
// count ๊ฐ’์€ 0์ด ๋ฉ๋‹ˆ๋‹ค.

 

self ํ”„๋กœํผํ‹ฐ

  • ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋Š” ์•”์‹œ์ ์œผ๋กœ ์ธ์Šคํ„ด์Šค ์ž์ฒด๋ฅผ ์˜๋ฏธํ•˜๋Š” 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" ์ถœ๋ ฅ

 

์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๊ฐ’ ํƒ€์ž… ๋ณ€๊ฒฝ

  • ๊ตฌ์กฐ์ฒด์™€ ์—ด๊ฒจํ˜•์€ ๊ฐ’ ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๊ฐ’ ํƒ€์ž…์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Œ
  • ํ•˜์ง€๋งŒ ๋ฉ”์„œ๋“œ์— mutating ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ”์„œ๋“œ์˜ ๊ณ„์‚ฐ์ด ๋๋‚œ ํ›„ ์›๋ณธ ๊ตฌ์กฐ์ฒด์— ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฎ์–ด ์จ์„œ ๊ฐ’์„ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
  • ์ƒ์ˆ˜ let์œผ๋กœ ์ธ์Šคํ„ด๋ฅผ ์„ ์–ธํ•˜๋ฉด 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)" ์ถœ๋ ฅ

let fixedPoint = Point(x: 3.0, y: 3.0)

fixedPoint.moveBy(x: 2.0, y: 3.0)
// let์œผ๋กœ ์„ ์–ธํ•ด์„œ ๊ฐ’ ๋ณ€๊ฒฝ ์‹œ๋„์‹œ ์—๋Ÿฌ ๋ฐœ์ƒ!

 

Mutating ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ self ํ• ๋‹น

  • Mutating๋ฉ”์„œ๋“œ์—์„œ selfํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•ด ์™„์ „ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ
struct Point {
    var x = 0.0, y = 0.0

    mutating func moveBy(x deltaX: Double, y deltaY: Double) {
        self = Point(x: x + deltaX, y: y + deltaY)
    }
}
enum TriStateSwitch {
    case off, low, high

    mutating func next() {
        switch self {
            case .off:
                self = .low

            case .low:
                self = .high

            case .high:
                self = .off
        }
    }
}

var ovenLight = TriStateSwitch.low

ovenLight.next()
// ovenLight ๊ฐ’์€ .high

ovenLight.next()
// ovenLight ๊ฐ’์€ .off

 

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

  • ํŠน์ • ํƒ€์ž… ์ž์ฒด์—์„œ ํ˜ธ์ถœํ•ด ์‚ฌ์šฉ
  • ํƒ€์ž… ๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ ํ‚ค์›Œ๋“œ func ์•ž์— static ํ˜น์€ class ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ์‚ฌ์šฉ
    • static ๋ฉ”์„œ๋“œ: ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ์ˆ˜ ์—†๋Š” ํƒ€์ž… ๋ฉ”์„œ๋“œ
    • class ๋ฉ”์„œ๋“œ: ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž… ๋ฉ”์„œ๋“œ
  • ํƒ€์ž… ๋ฉ”์„œ๋“œ๋„ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ์™€ ๊ฐ™์ด ์ (.) ๋ฌธ๋ฒ•์œผ๋กœ ํ˜ธ์ถœ
  • ํƒ€์ž… ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ self ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒ€์ž… ์ž์‹ ์„ ์˜๋ฏธ
class SomeClass {
    class func someTypeMethod() {
    // ํƒ€์ž… ๋ฉ”์„œ๋“œ ๊ตฌํ˜„
    }
}

SomeClass.someTypeMethod()    // ํƒ€์ž… ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ!
  • ํƒ€์ž… ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ๋‹ค๋ฅธ ํƒ€์ž… ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
struct LevelTracker {
    static var highestUnlockedLevel = 1
    var currentLevel = 1

    static func unlock(_ level: Int) {
        if level > highestUnlockedLevel { highestUnlockedLevel = level }
    }

    static func isUnlocked(_ level: Int) -> Bool {
        return level <= highestUnlockedLevel
    }

    @discardableResult //"์ด ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ๋‹ค"
    mutating func advance(to level: Int) -> Bool {
        if LevelTracker.isUnlocked(level) {
            currentLevel = level
            return true
        } else {
            return false
        }
    }
}
  • @discardableResult: ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์˜๋„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
    • ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ํ›„ ๋ฐ˜ํ™˜๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ปดํŒŒ์ผ๋Ÿฌ ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, “์ด ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ๋‹ค”๋Š” ์˜๋„๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ‘œํ˜„
    • ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋ฌธ๋ฒ•์ ์œผ๋กœ ๋ฌธ์ œ๋˜์ง€ ์•Š์Œ, ๊ฐœ๋ฐœ์ž์˜ ํŒ๋‹จ์— ๋”ฐ๋ผ ์‚ฌ์šฉ ํ˜น์€ ์ƒ๋žต ๊ฐ€๋Šฅ
class Player {
    var tracker = LevelTracker()
    let playerName: String

    func complete(level: Int) {
        LevelTracker.unlock(level + 1)
        tracker.advance(to: level + 1)
    }

    init(name: String) {
        playerName = name
    }
}

var player = Player(name: "Argyrios")

player.complete(level: 1)

print("highest unlocked level is now \\(LevelTracker.highestUnlockedLevel)")
// "highest unlocked level is now 2" ์ถœ๋ ฅ
// ๋ ˆ๋ฒจ 1์„ ์™„๋ฃŒํ•ด์„œ ๋ ˆ๋ฒจ 2๊ฐ€ ์—ด๋ ธ์Šต๋‹ˆ๋‹ค.

player = Player(name: "Beto")

if player.tracker.advance(to: 6) {
    print("player is now on level 6")
} else {
    print("level 6 has not yet been unlocked")
}

// "level 6 has not yet been unlocked" ์ถœ๋ ฅ
๋ฐ˜์‘ํ˜•

'โŒจ๏ธ Language > swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Swift] ์ƒ์†  (0) 2024.03.22
[Swift] ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ  (0) 2024.03.22
[Swift] ํ”„๋กœํผํ‹ฐ  (0) 2024.03.20
[Swift] ํด๋ž˜์Šค์™€ ๊ตฌ์กฐ์ฒด  (0) 2024.03.19
[Swift] ํด๋กœ์ €  (1) 2024.03.18