Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Swift/Swift Documents

[Swift] ๊ณต์‹ ๋ฌธ์„œ ์ •๋ฆฌ (11) - Methods (๋ฉ”์„œ๋“œ)

๊ฐ์ž ๐Ÿฅ” 2022. 2. 25. 23:07
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์ž์ž…๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ์Šค์œ„ํ”„ํŠธ ๊ณต์‹๋ฌธ์„œ 11์ฑ•ํ„ฐ์ธ, ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ์Šค์œ„ํ”„ํŠธ ์Šคํ„ฐ๋””๋ฅผ ํ•˜๋ฉด์„œ ๋‚˜๋ฆ„ ๋งค์ผ๋งค์ผ ๊พธ์ค€ํžˆ ์ •๋ฆฌ์ค‘์ธ๋ฐ์š”, ํ™•์‹คํžˆ ์Šคํ„ฐ๋””๋ฅผ ์šด์˜ํ•˜๋ฉฐ ์‚ฌ๋žŒ๋“ค๊ณผ ํ•จ๊ป˜ ๊ณต๋ถ€ํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ด ์˜ค๋ฅด๋Š” ๊ฒƒ๊ฐ™์•„์š”. ์–ผ๋ฅธ ๊ธฐ์ดˆ๊ณต๋ถ€๋ฅผ ๋๋‚ด๊ณ  ํ† ์ด ํ”„๋กœ์ ํŠธ๋„ ํ•จ๊ป˜ ํ•ด๋ณด๊ณ ์‹ถ์€ ๋งˆ์Œ์ด๋„ค์š”! ๊ทธ๋Ÿผ ์‹œ์ž‘

Swift Documents chap.11 Methods ๋ฐ”๋กœ๊ฐ€๊ธฐ

 

Methods — The Swift Programming Language (Swift 5.6)

Methods Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, struc

docs.swift.org


 

Methods

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

  • Instance Methods ํŠน์ • ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ
  • Type Methods ํŠน์ • ํƒ€์ž…๊ณผ ๊ด€๋ จ๋œ ๋ฉ”์„œ๋“œ

๐Ÿ‘‰๐Ÿป Swift ๋ฉ”์„œ๋“œ vs. Objective-C, C์˜ ๋ฉ”์„œ๋“œ

  • Objective-C: ํด๋ž˜์Šค์—์„œ๋งŒ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Œ
  • Swfit์—์„œ๋Š” ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด, ์—ด๊ฑฐํ˜• ๋ชจ๋‘์—์„œ ๋ฉ”์„œ๋“œ ์ •์˜ ๊ฐ€๋Šฅ

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์ด ๋ฉ๋‹ˆ๋‹ค.

โ–ท ์ฝ”๋“œ ์„ค๋ช…

  • Counterํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋กœ ๊ฐ๊ฐ increment(), increment(by amount: Int), reset()์„ ์ •์˜ํ•ด ์ธ์Šคํ„ด์Šค ๋‚ด์˜ count property๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

 

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

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

func increment() {
    self.count += 1
}

์•ž์˜ ์ฝ”๋“œ์—์„œ increment()๋ฉ”์„œ๋“œ์—์„œ๋Š” count+=1 ์ด๋ผ๊ณ  ํ‘œํ˜„ํ–ˆ์ง€๋งŒ, ์‚ฌ์‹ค self.count +=1 ์ด๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

swift์—์„œ๋Š” ํŠน์ • ๋ฉ”์„œ๋“œ์—์„œ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์— ๋“ฑ๋ก๋œ ๋ฉ”์„œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ˜„์žฌ ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์„œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ž๋™ ๊ฐ€์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ด ๊ทœ์น™์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š” ์˜ˆ์™ธ๊ฐ€ ์žˆ๋‹ค. ๋ฐ”๋กœ ์ธ์ž์ด๋ฆ„์ด ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์ด๋‹ค. ์ด ๊ฒฝ์šฐ์—๋Š” ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ช…์‹œ์ ์œผ๋กœ 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" ์ถœ๋ ฅ

โ–ท ์ถ”๊ฐ€ ์„ค๋ช…

  • ์ด ์ฝ”๋“œ์—์„œ๋Š” self.x๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœํผํ‹ฐ x์™€ ์ธ์ž x๋ฅผ ๊ตฌ๋ถ„ํ–ˆ๋‹ค.

 

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

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

๊ฐ’ ํƒ€์ž… ํ˜•์˜ ๋ฉ”์„œ๋“œ์—์„œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” 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)" ์ถœ๋ ฅ

โ–ท ์ถ”๊ฐ€ ์„ค๋ช…

  • ๊ตฌ์กฐ์ฒด๋ผ์„œ ๋ฉ”์„œ๋“œ๋กœ ์ธ์Šคํ„ด์Šค ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์ง€๋งŒ, mutating func moveBy()๊ฐ™์ด ๋ฉ”์„œ๋“œ ์•ž์— mutating์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ.
  • Point ๋‚ด์—์„œ var๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๊ธฐ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅ. (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)
    }
}

์—ด๊ฑฐํ˜•์—์„œ์˜ mutating ๋ฉ”์„œ๋“œ๋Š” self ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋™์ผํ•œ ์—ด๊ฑฐํ˜•์•ˆ์— ๋‹ค๋ฅธ ์ผ€์ด์Šค๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

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 ๋ฉ”์„œ๋“œ : ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž… ๋ฉ”์„œ๋“œ

ํƒ€์ž… ๋ฉ”์„œ๋“œ๋„ ์  ๋ฌธ๋ฒ•์œผ๋กœ ํ˜ธ์ถœํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ธ์Šคํ„ด์Šค์—์„œ ํ˜ธ์ถœํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๊ณ , ํƒ€์ž…๋ฉ”์„œ๋“œ ์ด๋ฆ„์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

class SomeClass {
    class func someTypeMethod() {
        // ํƒ€์ž… ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„
    }
}
SomeClass.someTypeMethod()    // ํƒ€์ž… ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ!

ํƒ€์ž…๋ฉ”์„œ๋“œ ์•ˆ์—์„œ๋„ self ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ self ์ธ์Šคํ„ด์Šค๊ฐ€ ์•„๋‹ˆ๋ผ, ํƒ€์ž… ์ž์‹ ์„ ์˜๋ฏธํ•œ๋‹ค. ๋˜ ํƒ€์ž… ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ๋‹ค๋ฅธ ํƒ€์ž… ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

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
        }
    }
}

 

โ–ท ์ฝ”๋“œ ์„ค๋ช…

  • ํƒ€์ž… ๋ฉ”์„œ๋“œ 3๊ฐœ์™€ mutating ๋ฉ”์„œ๋“œ 1๊ฐœ๋ฅผ ์„ ์–ธํ•ด ํ˜„์žฌ ๋ ˆ๋ฒจ๊ณผ ์ตœ๊ณ  ๋ ˆ๋ฒจ, ๋‹ค์Œ ๋ ˆ๋ฒจ ์—ฐ๊ธฐ, ๋‹ค์Œ ๋ ˆ๋ฒจ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.
  • ๊ฒŒ์ž„์—์„œ ์ตœ๊ณ  ๋ ˆ๋ฒจ์ด ์–ด๋””์ธ์ง€ ์ถ”์ ํ•˜๊ณ  ๋งŒ์•ฝ ๊ทธ ๋ ˆ๋ฒจ์ด ์—ด๋ ค ์žˆ์œผ๋ฉด ๊ทธ ์ชฝ์œผ๋กœ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ”๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” (advance) ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
  • advance ๋ฉ”์„œ๋“œ ์•ž์— ๋ถ™์€ @discardableResultํ‚ค์›Œ๋“œ๋Š” ๋ฆฌํ„ด ๊ฐ’์ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ์—์„œ ๋ฆฌํ„ด ๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ๊ทธ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•ด์ค€๋‹ค.
  • xx.advance ๋ผ๊ณ  ํ•˜๋ฉด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋‚˜์„œ bool ๊ฐ’์˜ ๋ฆฌํ„ด ๊ฐ’์„ ์–ด๋–ค ๋ณ€์ˆ˜์—๋„ ํ• ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, @discardableResult์ด๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ค˜์„œ ๋ง ๊ทธ๋Œ€๋กœ ์ด ๊ฒฐ๊ณผ ๊ฐ’์€ ๋ฒ„๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ฒฐ๊ณผ ๊ฐ’์ด์•ผ ๋ผ๊ณ  ํ‘œ์‹œ๋ฅผ ํ•ด์„œ ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

๋˜ ๋‹ค๋ฅธ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž.

์•„๋ž˜ ์ฝ”๋“œ๋Š” Playerํด๋ž˜์Šค์˜ complete๋ฉ”์†Œ๋“œ์—์„œ LevelTracker ์ธ์Šคํ„ด์Šค tracker์™€ ํƒ€์ž…๋ฉ”์†Œ๋“œ LevelTracker.unlock๋ฅผ ์‚ฌ์šฉํ•ด ํŠน์ • ์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ๋ ˆ๋ฒจ์„ ์ถ”์ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์ด๋‹ค.

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" ์ถœ๋ ฅ

๋ ˆ๋ฒจ 6์œผ๋กœ ๋ฐ”๋กœ ๊ฑด๋„ˆ๋›ฐ๋ ค ํ•˜์ง€๋งŒ, ์•„์ง ์—ด๋ฆฌ์ง€ ์•Š์•„์„œ ๊ด€๋ จ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

๋ฐ˜์‘ํ˜•