1. Methods ๐Ÿ‘ฉโ€๐Ÿ’ป

Methods๋Š” Functions ์ค‘์—์„œ ํŠน์ • Type ๊ณผ ์—ฐ๊ด€๋œ Functions ๋ฅผ ๋งํ•œ๋‹ค.

Classes, Structures, Enumerations ๋ชจ๋‘ Instance ์˜ ์ž‘๋™์„ ์œ„ํ•œ Instance Methods๋ฅผ ์ •์˜ํ•˜๊ณ , Encapsulate(์บก์Šํ™”) ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ Type์„ ์œ„ํ•œ Type Methods ์—ญ์‹œ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์€ Objective-C ์˜ Class Methods ์™€ ์œ ์‚ฌํ•˜๋‹ค.

Objective-C ์—์„œ Classes ๋Š” Methods ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ํƒ€์ž…์ธ ๋ฐ˜๋ฉด, Swift ๋Š” Classes ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ Structures ์™€ Enumerations ์—์„œ๋„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์œ ์—ฐ์„ฑ์„ ๋†’์˜€๋‹ค.


2. Instance Methods ๐Ÿ‘ฉโ€๐Ÿ’ป

Instance Methods ๋Š” Classes, Structures, Enumerations ์˜ Instance ์— ์†ํ•ด ์žˆ๋Š” ํ•จ์ˆ˜๋กœ, Instance ์˜ Properties ์— ์ ‘๊ทผ, ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ Instance ์˜ ์ž‘๋™์„ ์œ„ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

Instance Methods ๋Š” ๊ทธ๊ฒƒ์ด ์ •์˜๋œ context ๋‚ด์˜ ๋‹ค๋ฅธ ๋ชจ๋“  Instance Methods ์™€ Instance Properties ์— ๋Œ€ํ•ด ์•”์‹œ์ ์ธ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๊ฐ–๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  Instance Methods ๋Š” Instance Properties ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Instance ์—†์ด ๋…๋ฆฝ์ ์œผ๋กœ ํ˜ธ์ถœ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.


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

Counter Class ๋ฅผ ์ •์˜ํ•˜๊ณ , Counter ํƒ€์ž…์˜ counter instance ๋ฅผ ์„ ์–ธํ–ˆ๋‹ค.
Instance Methods ๋Š” Instance Properties ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ dot Syntax๋ฅผ ์ด์šฉํ•ด ํ˜ธ์ถœํ•œ๋‹ค.

print(counter.count)    // 0

counter.increment()
print(counter.count)    // 1

counter.increment(by: 5)
print(counter.count)    // 6

counter.reset()
print(counter.count)    // 0

1. The self Property

Instance ๋Š” self๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” Instance ์ž๊ธฐ ์ž์‹ ๊ณผ ๋™์ผํ•œ Property๋ฅผ ์•”์‹œ์ ์œผ๋กœ ๊ฐ–๋Š”๋‹ค(implicit self property).

์›๋ž˜ Methods ๊ฐ€ ์ž๊ธฐ ์ž์‹ ์˜ context ์™ธ๋ถ€, ์ฆ‰ ์œ„ ๊ฒฝ์šฐ Counter Class ์˜ context ์— ์ •์˜๋œ Instance Properties ๋‚˜ ๋‹ค๋ฅธ Instance Methods ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค.

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

ํ•˜์ง€๋งŒ Swift ๋Š” context ๋‚ด์—์„œ ๋Œ€์ƒ์„ ์ฐพ์ง€ ๋ชป ํ•  ๊ฒฝ์šฐ, ํ˜„์žฌ Instance ์˜ context ์—์„œ ์ฐพ๋„๋ก ์•”์‹œ์ ์œผ๋กœ self๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ, ๋ช…์‹œ์ ์œผ๋กœ self ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค.

func increment() {
    count += 1
}

๊ทธ๋Ÿฌ๋‚˜ Methods ์˜ parameters ์™€ properties ๊ฐ€ ์ด๋ฆ„์ด ์ค‘๋ณต๋  ๊ฒฝ์šฐ parameters ๊ฐ€ ์šฐ์„ ๊ถŒ์„ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ์•”์‹œ์  self๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ ๋ช…์‹œ์ ์œผ๋กœ self๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผํ•œ๋‹ค.

struct Point {
    var x = 0.0, y = 0.0
    func isToTheRightOf(x: Double) -> Bool {
        self.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

2. Modifying Value Types from Within Instance Methods

Structures ์™€ Enumerations ๋Š” Value Types๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ Value Type ์˜ Properties ๋Š” Instance Methods ์— ์˜ํ•ด ์ˆ˜์ •๋  ์ˆ˜ ์—†๋‹ค(immutable).

์ˆ˜์ •์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ mutating ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ˆ˜์ •์„ ํ—ˆ์šฉํ•˜๋„๋ก ๋ช…์‹œํ•ด์•ผํ•œ๋‹ค.
mutating Methods ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ Properties ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค. ๋˜ํ•œ ์ด ๋ฉ”์„œ๋“œ๋Š” implicit self property์— ์™„์ „ํžˆ new Instance๋ฅผ ํ• ๋‹นํ•  ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, 'new Instance'๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ 'original Instance'๋ฅผ ๋Œ€์ฒดํ•œ๋‹ค.

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)
print("The point is at (\(somePoint.x), \(somePoint.y))")

somePoint.moveBy(x: 2.0, y: 3.0)
print("The point is now at (\(somePoint.x), \(somePoint.y))")
The point is at (1.0, 1.0)
The point is now at (3.0, 4.0)

mutating ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด Structures ์˜ Properties ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์€ Structure Instance ๋ฅผ var๋กœ ์„ ์–ธํ•œ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
Stored Properties of Constant Structure Instances

3. Assigning to self Within a Mutating Method

์œ„ 2๋ฒˆ์—์„œ mutating Methods ๊ฐ€ Properties ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ์˜ˆ๋ฅผ ๋ณด์•˜๋‹ค. ์ด๋ฒˆ์—๋Š” implicit self property์— ์™„์ „ํžˆ new Instance๋ฅผ ํ• ๋‹นํ•ด original Instance๋ฅผ ๋Œ€์ฒด ํ•˜๋Š” ๋กœ์ง์„ ์‚ดํŽด๋ณธ๋‹ค.

struct Point {
    var x = 0.0, y = 0.0
    mutating func moveBy(x deltaX: Double, y deltaY: Double) {
        x += deltaX
        y += deltaY
    }
    mutating func anotherMoveBy(x deltaX: Double, y deltaY: Double) {
        self = Point(x: x + deltaX, y: y + deltaY)
    }
}

anotherMoveBy(x:y:)๋Š” self, ์ฆ‰, Instance ์ž๊ธฐ ์ž์‹  ์„ Point(x:y)๋ฅผ ์ด์šฉํ•ด ์ƒˆ Instance ๋ฅผ ์ƒ์„ฑํ•œ ํ›„, ๊ธฐ์กด์˜ Instance ๋ฅผ ๋Œ€์ฒดํ•œ๋‹ค.

var somePoint = Point(x: 1.0, y: 1.0)
print("The point is at (\(somePoint.x), \(somePoint.y))")

somePoint.moveBy(x: 2.0, y: 3.0)
print("The point is now at (\(somePoint.x), \(somePoint.y))")

somePoint.anotherMoveBy(x: 5.0, y: 2.0)
print("The point is now at (\(somePoint.x), \(somePoint.y))")
The point is at (1.0, 1.0)
The point is now at (3.0, 4.0)
The point is now at (8.0, 6.0)


๊ทธ๋ฆฌ๊ณ  ๊ณผ์—ฐ ์ด๋•Œ new Instance ๋ฅผ ์ด์šฉํ•ด original Instance ๋ฅผ ๋Œ€์ฒดํ•˜๋ฉด somePoint์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ๋ฐ”๋€Œ๋Š”์ง€๋„ ํ•จ๊ป˜ ํ™•์ธํ•ด ๋ณด์•˜์œผ๋‚˜ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

func address(of object: UnsafeRawPointer) -> NSString {
    let address = Int(bitPattern: object)
    return NSString(format: "%p", address)
}
var somePoint = Point(x: 1.0, y: 1.0)
print("Point's memory address is \(address(of: &somePoint))")

somePoint.moveBy(x: 2.0, y: 3.0)
print("Point's memory address is \(address(of: &somePoint))")

somePoint.anotherMoveBy(x: 5.0, y: 2.0)
print("Point's memory address is \(address(of: &somePoint))")
Point's memory address is 0x10377c840
Point's memory address is 0x10377c840
Point's memory address is 0x10377c840

3. Type Methods ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Type Method Syntax

Type Property Syntax ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Methods ์•ž์— static ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

struct SomeStructure {
    static func someTypeMethod() {
        // type method implementation goes here
    }
}

Type Methods ์—์„œ self๋Š” Instance ๊ฐ€ ์•„๋‹Œ Type itself, ์ฆ‰ Type ์ž์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Instance Methods ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, self๋ฅผ ์•”์‹œ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ Type ์˜ context ์— ์ •์˜๋œ Type Properties ๋‚˜ Type Methods ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ self๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐจ์ด์ ์ด ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • Instance Methods๋Š” context ๋‚ด๋ถ€์— ์ •์˜๋œ Instance Properties์™€ Instance Methods์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.
    ๋˜ํ•œ Type Methods ์ ‘๊ทผ๋„ ๊ฐ€๋Šฅํ•œ๋ฐ, Type ์˜ full name์„ ๋ถ™์—ฌ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • Type Methods๋Š” context ๋‚ด๋ถ€์— ์ •์˜๋œ Type Properties์™€ Type Methods์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.

2. Type Method Examples

๊ฒŒ์ž„์„ ํ•˜๋‚˜ ๋งŒ๋“ค์ž. ์ด ๊ฒŒ์ž„์—๋Š” ๊ฒŒ์ž„ ๋ ˆ๋ฒจ์ด ์กด์žฌํ•˜๋ฉฐ, ๊ฒŒ์ž„์˜ ๋ ˆ๋ฒจ์€ 1์—์„œ ์‹œ์ž‘ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ”Œ๋ ˆ์ด์–ด ์ค‘ ๋ˆ„๊ตฐ๊ฐ€ ๊ฒŒ์ž„์˜ ๋ ˆ๋ฒจ์„ ํ•ด์ œํ•˜๋ฉด ๊ฒŒ์ž„์˜ ์ตœ๊ณ  ๋ ˆ๋ฒจ์ด ์˜ฌ๋ผ๊ฐ€๊ณ , ๋ชจ๋“  ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ํ•ด๋‹น ๋ ˆ๋ฒจ์˜ ๊ฒŒ์ž„์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

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
    }
    
    mutating func advance(to level: Int) -> Bool {
        if LevelTracker.isUnlocked(level) {
            currentLevel = level
            return true
        } else {
            return false
        }
    }
}
  • highestUnlockedLevel : ํ”Œ๋ ˆ์ด ๊ฐ€๋Šฅํ•œ ๊ฒŒ์ž„์˜ ์ตœ๊ณ  ๋ ˆ๋ฒจ.
  • currentLevel: ํ˜„์žฌ ๊ฒŒ์ž„ ๋ ˆ๋ฒจ.
  • unlock(_:) : ๋‹ค์Œ ๋ ˆ๋ฒจ ๊ฒŒ์ž„์„ ํ•ด์ œํ•œ๋‹ค. ์ฆ‰, ํ”Œ๋ ˆ์ด ๊ฐ€๋Šฅํ•œ ๊ฒŒ์ž„์˜ ์ตœ๊ณ  ๋ ˆ๋ฒจ์ด ์˜ฌ๋ผ๊ฐ„๋‹ค.
  • isUnlocked(to:) : ๊ฒŒ์ž„ ๋ ˆ๋ฒจ์ด ํ•ด์ œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  • advance(to:) : ๊ฒŒ์ž„ ๋ ˆ๋ฒจ์„ ์˜ฌ๋ฆฐ๋‹ค.


์ด์ œ ์ด ๊ฒŒ์ž„ ๊ทœ์น™์„ ์ ์šฉํ•ด ํ”Œ๋ ˆ์ด๋ฅผ ์ง„ํ–‰ํ•  ๊ฒŒ์ž„ ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๋งŒ๋“ ๋‹ค.

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
    }
}
  • tracker : ๊ฒŒ์ž„์„ ๊ทœ์น™์„ ์ €์žฅํ•œ LevelTracker ๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ–๋Š”๋‹ค.
  • complete(level:) : ํ˜„์žฌ ๋ ˆ๋ฒจ์˜ ๊ฒŒ์ž„์„ ์™„๋ฃŒํ•˜๋ฉด LevelTracker ์˜ unlock(_:) ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๊ฒŒ์ž„์˜ ์ตœ๊ณ  ๋ ˆ๋ฒจ์„ ํ•ด์ œํ•˜๊ณ , ํ˜„์žฌ ๊ฒŒ์ž„ ๋ ˆ๋ฒจ์„ ์˜ฌ๋ฆฐ๋‹ค.

์—ฌ๊ธฐ์„œ ์ฃผ๋ชฉํ•ด์•ผ ํ•  ์ ์€ LevelTracker ๋Š” Structure ๊ณ , Player ๋Š” Class ๋ผ๋Š” ์ ์ด๋‹ค. ๋˜ํ•œ ๊ฒŒ์ž„ ์„ค์ •์˜ LevelTracker ๋ฅผ ๋ณด๋ฉด highestUnlockedLevel ๋Š” Type Property ๊ณ , currentLevel ์€ Instance Property ๋‹ค.

์ฆ‰, ์–ด๋–ค ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๊ฒŒ์ž„์˜ ์ตœ๊ณ  ๋ ˆ๋ฒจ์„ ๋†’์—ฌ ๋†“์œผ๋ฉด ๊ทธ ๋‹ค์Œ ํ”Œ๋ ˆ์ด์–ด๋Š” ํ•ด๋‹น ๋ ˆ๋ฒจ์„ ์ž ๊ธˆ์ด ํ•ด์ œ๋œ ๊ฒŒ์ž„์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•˜๋ฉฐ, ์ตœ๊ณ  ๋ ˆ๋ฒจ ํ•ด์ œ์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ƒˆ ํ”Œ๋ ˆ์ด์–ด๋Š” ํ•ญ์ƒ ๋ ˆ๋ฒจ 1๋กœ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ด์•ผํ•จ์„ ์˜๋ฏธํ•œ๋‹ค.


๊ฒŒ์ž„์„ ์ง„ํ–‰ํ•ด๋ณด์ž.

var room1 = Player(name: "Harry")
print("\(room1.tracker), Harry is playing level \(room1.tracker.currentLevel).")
LevelTracker(currentLevel: 1), Harry is playing level 1.

ํ•ด๋ฆฌ๊ฐ€ ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๋งŒ๋“ค๊ณ  1๋ฒˆ ๋ฐฉ์—์„œ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ–ˆ๋‹ค. ํ•ด๋ฆฌ๊ฐ€ ์ง„ํ–‰์ค‘์ธ ๊ฒŒ์ž„ ๋ ˆ๋ฒจ์€ 1์ด๋‹ค.

room1.complete(level: 1)
print("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
print("\(room1.tracker), Harry is playing level \(room1.tracker.currentLevel).")
highest unlocked level is now 2
LevelTracker(currentLevel: 2), Harry is playing level 2.

ํ•ด๋ฆฌ๊ฐ€ ๊ฒŒ์ž„์„ ์™„๋ฃŒํ•ด ๋‹ค์Œ ๋ ˆ๋ฒจ์„ ํ•ด์ œํ–ˆ๋‹ค.
์ด์ œ ๊ฒŒ์ž„์˜ ์ตœ๊ณ  ๋ ˆ๋ฒจ์€ 2๊ณ , ํ•ด๋ฆฌ๋Š” 1๋ฒˆ ๋ฐฉ์—์„œ ๋ ˆ๋ฒจ 2๋ฅผ ์ง„ํ–‰์ค‘์ด๋‹ค.


var room2 = Player(name: "Ron")
print("\(room2.tracker), Ron is playing level \(room2.tracker.currentLevel).")
LevelTracker(currentLevel: 1), Ron is playing level 1.

๋ก ์ด ์ƒˆ๋กœ์šด ํ”Œ๋ ˆ์ด์–ด๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ๋ก ์€ ๊ฒŒ์ž„๋ฐฉ๋„ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด 2๋ฒˆ๋ฐฉ์—์„œ ๊ฒŒ์ž„์„ ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.
๊ฒŒ์ž„ ์„ค์ • LevelTracker ์— ์˜ํ•ด ๋ก  ์—ญ์‹œ ๋ ˆ๋ฒจ 1๋กœ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•œ๋‹ค.

if LevelTracker.isUnlocked(2) {
    room2.tracker.advance(to: 2)
}
print("\(room2.tracker), Ron is playing level \(room2.tracker.currentLevel).")
LevelTracker(currentLevel: 2), Ron is playing level 2.

๊ทธ๋Ÿฐ๋ฐ ๋ก ์€ ๋ ˆ๋ฒจ 1์€ ์ง€๋ฃจํ•˜๋‹ค๋ฉฐ ๋ ˆ๋ฒจ 2๊ฐ€ ํ•ด์ œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ ํ›„, ๋ ˆ๋ฒจ์„ ์˜ฌ๋ฆฌ๊ธฐ๋กœ ํ–ˆ๊ณ  ์„ฑ๊ณตํ–ˆ๋‹ค.
๊ทธ ๊ฒฐ๊ณผ ๋ก ์€ 2๋ฒˆ ๋ฐฉ์—์„œ ๋ ˆ๋ฒจ 2๋ฅผ ์ง„ํ–‰์ค‘์ด๋‹ค.


room1 = Player(name: "Hermione")
print("\(room1.tracker), Hermione is playing level \(room1.tracker.currentLevel).")
LevelTracker(currentLevel: 1), Hermione is playing level 1.

์ด๋ฒˆ์—๋Š” ํ—ค๋ฅด๋ฏธ์˜จ๋Š๊ฐ€ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•˜๋Š”๋ฐ, ํ—ค๋ฅด๋ฏธ์˜จ๋Š๋Š” ํ•ด๋ฆฌ๊ฐ€ ํ•˜๋˜ 1๋ฒˆ๋ฐฉ์—์„œ ๊ฒŒ์ž„์„ ์ด์–ด์„œ ํ•˜๊ธฐ๋กœ ํ•˜๊ณ  ํ”Œ๋ ˆ์ด์–ด๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด ๊ฒŒ์ž„์— ๋“ค์–ด๊ฐ”๋‹ค. 1๋ฒˆ ๋ฐฉ์—์„œ ํ•ด๋ฆฌ๋Š” ๋ ˆ๋ฒจ 2๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์—ˆ์ง€๋งŒ ํ—ค๋ฅด๋ฏธ์˜จ๋Š๋Š” ์ƒˆ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•˜๋Š” ํ”Œ๋ ˆ์ด์–ด์ด๋ฏ€๋กœ ๊ฒŒ์ž„ ์„ค์ • LevelTracker ์— ์˜ํ•ด ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ ˆ๋ฒจ 1์—์„œ ์‹œ์ž‘ํ•œ๋‹ค.

print("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
if room1.tracker.advance(to: 6) {
    print("Hermione is now on level 6")
} else {
    print("level 6 hasn't yet been unlocked")
}
highest unlocked level is now 2
level 6 hasn't yet been unlocked

๊ทธ๋Ÿฐ๋ฐ ํ—ค๋ฅด๋ฏธ์˜จ๋Š๋Š” ๋‚ฎ์€ ๋ ˆ๋ฒจ์˜ ๊ฒŒ์ž„์€ ์žฌ๋ฏธ ์—†๋‹ค๋ฉฐ ํ•œ ๋ฒˆ์— ๋ ˆ๋ฒจ 6์œผ๋กœ ์˜ฌ๋ฆฌ๊ธฐ๋ฅผ ์›ํ–ˆ๋‹ค. ํ˜„์žฌ ๊ฒŒ์ž„ ํ”Œ๋ ˆ์ด๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ตœ๊ณ  ๋ ˆ๋ฒจ์ด 2๋ผ๋Š” ๊ฒƒ์„ ํ™•์ธ ํ–ˆ์œผ๋‚˜ ํ˜น์‹œ๋‚˜ ํ•˜๋Š” ๋งˆ์Œ์— ๋ ˆ๋ฒจ 6์œผ๋กœ ์˜ฌ๋ฆฌ๊ธฐ๋ฅผ ์‹œ๋„ํ–ˆ๊ณ , ๋ ˆ๋ฒจ 6์€ ์•„์ง ์ž ๊ฒจ์žˆ๋‹ค๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์•˜๋‹ค.


Reference

  1. โ€œMethods.โ€ The Swift Programming Language Swift 5.7. accessed Nov. 27, 2022, Swift Docs Chapter 10 - Methods.