티스토리 뷰

iOS 개발/Swift

[Swift 3] 메소드 (Method)

beankhan 2017. 4. 15. 17:37

출처

http://kka7.tistory.com/16



메소드

구조체와 열거형에 메소드를 정의할 수 있는 것이
Objective-C 와의 큰 차이점이다.

Swift 에서는 클래스, 구조체, 열거형에서 추가할 수 있다.



인스턴스 메소드

인스턴스 메소드는 함수와 같은 문법을 가진다.

class Counter {

    var count = 0

    func increment() {

        count += 1

    }

    func increment(by amount: Int) {

        count += amount

    }

    func reset() {

        count = 0

    }

}


let counter = Counter()

counter.increment()

counter.increment(by: 10)

counter.reset()




self 프로퍼티 

타입의 인스턴스는 인스턴스 자신과 똑같은
암시적으로 self 프로퍼티를 가진다.

func increment() {

    self.count += 1

}



struct Point {

    var x = 0.0, y = 0.0

    func isToTheRightOf(x: Double) -> Bool {

        return 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")

}





인스턴스 메소드 내에서 값 타입 수정

구조체와 열거형은 값 타입이다.

기본적으로 값 타입의 프로퍼티는 인스턴스 메소드 내에서 수정될 수 없다.


하지만 프로퍼티가 특정 메소드 내에서 수정이 필요하다면

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))")

// Prints "The point is now at (3.0, 4.0)"



구조체에서 프로퍼티 값의 변경을 위해서는

mutating 을 함수 앞에 써준다.


구조체 타입의 상수는 mutating 로 정의된 함수를 호출할 수 없다.

아래와 같이 호출하면 컴파일 오류가 난다.


let somePoint = Point(x: 1.0, y: 1.0)

somePoint.moveBy(x: 2.0, y: 3.0)



클래스의 경우는 mutating 키워드를 사용할 수 없다.

사용하지 않고도 값을 변경할 수 있다.




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)

    }

}


self 매개변수는 같은 열거형의 다른 case 값으로 설정할 수 있다.


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 is now equal to .high


ovenLight.next()

// ovenLight is now equal to .off





타입 메소드 (Type Method)

타입 메소드는 메소드의 func 키워드 앞에 
static 키워드를 작성하여 나타낸다.

클래스는 하위 클래스가 상위 클래스의 구현을 오버라이드 하는 것을
허용하기 위해 class 키워드를 사용할 수 있다.


class SomeClass {

    class func someTypeMethod() {

        // type method implementation goes here

    }

}

SomeClass.someTypeMethod()



@discardableResult 속성은 추후 attribute 에서 확인한다.


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

        }

    }

}



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)")

// Prints "highest unlocked level is now 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")

}




'iOS 개발 > Swift' 카테고리의 다른 글

[Swift 3] 상속 (Inheritance)  (0) 2017.04.15
[Swift 3] 서브스크립트 (Subscript)  (0) 2017.04.15
[Swift 3] 프로퍼티 (Property)  (0) 2017.04.15
[Swift 3] 클래스와 구조체  (1) 2017.04.14
[Swift 3] 열거형 (Enumerations)  (0) 2017.04.14
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함