yyeonjju / Interview_Questions

0 stars 0 forks source link

Swift가 함수를 실행시키는 3가지 방법과 특징 #6

Open yyeonjju opened 9 months ago

yyeonjju commented 9 months ago

1.Direct Dispatch (직접/Static Dispatch)

🌸 코드예시
```swift struct MyStruct { func method1() { print("Struct - Direct method1") } //10-20 func method2() { print("Struct - Direct method2") } //30-40 } let struct = MyStruct() struct.method1() // 10 : 여기 코드에다가 실행되어야할 코드 영역의 메모리 주소(명령어 주소)를 바로 삽입해놓거나, 명령코드를 직접적으로 심어버린다 struct.method2() // 30 ```







2.Table Dispatch (동적/Dynamic Dispatch)

🌸 코드예시
```swift class FirstClass { //Table Dispatch - Virtual Table : [110, 120] func method1() { print("Class - Table method1") } //코드 영역 110 - 119 func method2() { print("Class - Table method2") } //코드 영역 120 - 129 } // 자식클래스에서 테이블을 따로 보유 class SecondClass: FirstClass { //Table Dispatch - Virtual Table : [110, 130, 140] override func method2() { print("Class - Table method2-2") } //코드 영역 130-139 func method3() { print("Class - Table method3") } //코드 영역 140-149 } let first = FirstClass() first.method1() //110 으로 가서 실행 first.method2() //120 으로 가서 실행 let second = SecondClass() second.method1() //110 으로 가서 실행 second.method2() //130 으로 가서 실행 (재정의된 것) second.method3() //140 으로 가서 실행 (상속 후 새로운 메서드) ``` ```swift protocol MyProtocol { //Table Dispatch - Witness Table : [210, 220] func method1() // 요구사항 - 코드 영역 210 - 219 func method2() // 요구사항 - 코드 영역 220 - 229 } extension MyProtocol { // 요구사항의 기본 구현 제공 ==> Witness Table : [210, 220] func method1() { print("Protocol - Witness Table method1") } func method2() { print("Protocol - Witness Table method2") } // 필수 요구사항은 아님 ==> Direct Dispatch func anotherMothod() { print("Protocol Extension - Direct method") } } class FirstClass: MyProtocol { func method1() { print("Class - Virtual Table method1") } func method2() { print("Class - Virtual Table method2") } func anotherMothod() { print("Class - Virtual Table method3") } } let first = FirstClass() //클래스 타입으로 저장 => Virtual Table first.method1() // Class - Virtual Table method1 first.method2() // Class - Virtual Table method2 first.anotherMothod() // Class - Virtual Table method3 let proto: MyProtocol = FirstClass() //프로토콜 타입으로 저장 => // 본체 필수 요구 사항에 있는 건 Witness Table // 본체 필수 요구 사항에 없으면서확장으로 기본 구현된건 Direct method //⭐️ But, FirstClass에서 프로토콜 채택하고 메서드들을 재구현해주었기 때문에 Witness Table이 아닌 Virtual Table로 저장/실행된다 //⭐️ 만약, FirstClass에서 프로토콜 채택하고 메서드들을 재구현해주지 않았다면 Witness Table 형태로 저장/실행되었겠지 proto.method1() // Class - Virtual Table method1 proto.method2() // Class - Virtual Table method2 proto.anotherMothod() // Protocol Extension - Direct method 여기에바로 명령어 주소를 삽입하거나 명령어를 삽입 ```







3.Message Dispatch

🌸 코드예시
```swift class ParentClass { @objc dynamic func method1() { print("Class - Message method1") } @objc dynamic func method2() { print("Class - Message method2") } } class ChildClass: ParentClass { @objc dynamic override func method2() { print("Class - Message method2-2") } @objc dynamic func method3() { print("Class - Message method3") } } ```
yyeonjju commented 9 months ago
yyeonjju commented 8 months ago

https://maxim-kryloff.medium.com/swift-method-dispatch-4ac7efab0388

구조체도 Direct Dispatch가 아닌 Table Dispatch 를 사용할 수 있다??!! ( ⇒다형성 가능해짐 ) - 구조체에서 프로토콜 채택 시!! Virtual Table(X) Witness Table(O)