Language/Swift

[2_객체지향 프로그래밍과 스위프트] Chapter10. property, method

개랭갱깽스타 2021. 3. 11. 11:48

property: 클래스, 구조체, 열거형 등에 관련된 값

- 저장 프로퍼티

- 지연저장

- 연산 프로퍼티

- 타입 프로퍼티

+ 프로퍼티 감시자

더보기
//프로퍼티 감시자 = 프로퍼티의 값이 새로 할당될 때마다 호출
class Account {
    var credit: Int = 0 {   //저장 프로퍼티
        willSet {
            print("잔액이 \(credit)원에서 \(newValue)원으로 변경될 예정입니다.")
        }
        didSet {
            print("잔액이 \(oldValue)원에서 \(credit)원으로 변경되었습니다.")
        }
    }
    
    var dollarValue: Double {
        get {   //연산 프로퍼티
            return Double(credit)
        }
        
        set {
            credit = Int(newValue * 1000)
            print("잔액을 \(newValue * 1000)달러로 변경 중입니다.")
        }
    }
}

lazy

+ 키경로(Key Path): 프로퍼티의 값을 바로 꺼내오는 것이 아니라, 어떤 프로퍼티의 위치만 참조할 수 있도록. AnyKeyPath 클래스로부터 파생

 

method: 특정 타입에 관련된 함수

더보기

(함수를 상수/변수에 참조하기)

func someFunction(_ paramA: Any, _ paramB: Any) {
    print("some Function called...")
}

func anotherFunction(_ param: Any, _ paramB: Any) {
    print("another Function called...")
}

func otherFunction() {
    print("other Function called...")
}

var functionReference = someFunction
functionReference("A", "B")
functionReference = anotherFunction
functionReference("1", "2")

//functionReference = otherFunction //error
반응형

'Language > Swift' 카테고리의 다른 글

[Optional]  (0) 2021.05.12
[Optional]  (0) 2021.05.10
[2_객체지향 프로그래밍과 스위프트] Chapter9. struct/class  (0) 2021.03.11
[1_스위프트 기초] Chpater8. Optional  (0) 2021.03.11
[1_스위프트 기초] Chaptrer 1. Swift  (0) 2021.03.11