iOS/🟠 Swift

Adopting a Protocol Using a Synthesized Implementation - Protocols

woozzang 2021. 1. 27. 23:40

μŠ€μœ„ν”„νŠΈλŠ” μ—¬λŸ¬ κ°„λ‹¨ν•œ ν΄λž˜μŠ€λ“€μ—κ²Œ μžλ™μ μœΌλ‘œ `Equatable`, `Hashable` 그리고 `Comparable` ν”„λ‘œν† μ½œ μˆœμ‘(conformance) 을 μ œκ³΅ν•©λ‹ˆλ‹€.

`conform to protocol`을 ν•˜κΈ° μœ„ν•œ κ΅¬ν˜„μ΄ μžλ™μœΌλ‘œ μ œκ³΅λœλ‹€λŠ” μ˜λ―Έμž…λ‹ˆλ‹€.

μ—¬λŸ¬ κ°„λ‹¨ν•œ 클래슀: Numbers, String  λ“±μ΄ μžˆμŠ΅λ‹ˆλ‹€.

  +) Numbers. String 은 Foundation Framework 의  primitive type μž…λ‹ˆλ‹€.

 

이 ν†΅ν•©λœ κ΅¬ν˜„μ„ μ‚¬μš©ν•œλ‹€λŠ” 것은 ν”„λ‘œν† μ½œ 채택을 μœ„ν•œ λ³„λ„μ˜ 반볡적인 μƒμš©μ½”λ“œ(boilerplate) κ΅¬ν˜„μ΄ ν•„μš” μ—†λ‹€λŠ” 것을 μ˜λ―Έν•©λ‹ˆλ‹€.

ν†΅ν•©λœ κ΅¬ν˜„ : synthesized implementation

ν”„λ‘œν† μ½œμ˜ μ„ μ–Έκ³Ό κ΅¬ν˜„μ΄ λͺ¨λ‘ λ˜μ–΄μžˆμ–΄μ„œ μ±„νƒν•˜λ €λŠ” νƒ€μž…μ— μ„ μ–Έλ§Œ ν•˜λ©΄ λ˜λ―€λ‘œ, ν†΅ν•©λ˜μ—ˆλ‹€λŠ” ν‘œν˜„μ„ μ‚¬μš©ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€.

 


 

μŠ€μœ„ν”„νŠΈλŠ” λ‹€μŒκ³Ό 같은 μ’…λ₯˜μ˜ Custom type에 λŒ€ν•΄  `Equatable` ν†΅ν•©λœ κ΅¬ν˜„μ„ μ œκ³΅ν•©λ‹ˆλ‹€:

  • μ €μž₯ μ†μ„±λ§Œμ„ κ°€μ§€λ©΄μ„œ, κ·Έ 속성듀이 λͺ¨λ‘ `Equatable`을 λ”°λ₯΄λŠ”(conform) ꡬ쑰체

  • `Equatable`을 λ”°λ₯΄λŠ” associated type λ§Œμ„ κ°€μ§€λŠ” μ—΄κ±°ν˜•

  • associated type이 μ—†λŠ” μ—΄κ±°ν˜•

`==`  μ—°μ‚°μžμ— λŒ€ν•œ ν†΅ν•©λœ κ΅¬ν˜„μ„ λ°›μœΌλ €λ©΄, 원본 νƒ€μž… μ„ μ–Έμ—μ„œ `Equatable` μ±„택을 μ„ μ–Έν•΄μ•Ό ν•©λ‹ˆλ‹€.

 

그리고 직접 `==`  μ—°μ‚°μžλ₯Ό κ΅¬ν˜„ν•˜μ§€ μ•Šμ•„λ„ λ©λ‹ˆλ‹€.

 

`Equatable` ν”„λ‘œν† μ½œμ€ `!=` μ—°μ‚°μžμ˜ κ΅¬ν˜„λ„ μ œκ³΅ν•©λ‹ˆλ‹€.

 

μ•„λž˜ Vector3D ꡬ쑰체 λŠ” 3차원 벑터 ν‘œν˜„μ„ μœ„ν•œ (x, y, z) μ €μž₯ 속성을 가지고 μžˆμŠ΅λ‹ˆλ‹€.

x, y, z 속성은 λͺ¨λ‘ `Equatable` νƒ€μž… 이기 λ•Œλ¬Έμ—, Vector3D λŠ” `==`  , `!=`  μ—°μ‚°μž(equivalence operators) λ₯Ό μ œκ³΅λ°›μŠ΅λ‹ˆλ‹€.

κΈ€ μœ„μͺ½μ˜ `μ €μž₯μ†μ„±λ§Œμ„ κ°€μ§€λ©΄μ„œ, κ·Έ 속성듀이 λͺ¨λ‘ `Equatable`을 λ”°λ₯΄λŠ”(conform) ꡬ쑰체`에 ν•΄λ‹Ήλ©λ‹ˆλ‹€.

ν”„λ‘œν† μ½œμ€ νƒ€μž…μ΄κΈ° λ•Œλ¬Έμ—, κ·Έ ν”„λ‘œν† μ½œμ„ μ±„νƒν•œ νƒ€μž…λ„ ν•΄λ‹Ή ν”„λ‘œν† μ½œ νƒ€μž…μœΌλ‘œ μ·¨κΈ‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ”°λΌμ„œ Vector3DλŠ” `Equatable` νƒ€μž…μ΄λΌκ³  μ·¨κΈ‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

struct Vector3D: Equatable {
    var x = 0.0, y = 0.0, z = 0.0
}

let twoThreeFour = Vector3D(x: 2.0, y: 3.0, z: 4.0)
let anotherTwoThreeFour = Vector3D(x: 2.0, y: 3.0, z: 4.0)
if twoThreeFour == anotherTwoThreeFour {
    print("These two vectors are also equivalent.")
}
// Prints "These two vectors are also equivalent."

 

μŠ€μœ„ν”„νŠΈλŠ” λ‹€μŒκ³Ό 같은 μ’…λ₯˜μ˜ Custom type에 λŒ€ν•΄  `Hashable` ν†΅ν•©λœ κ΅¬ν˜„μ„ μ œκ³΅ν•©λ‹ˆλ‹€:

 

  • μ €μž₯μ†μ„±λ§Œμ„ κ°€μ§€λ©΄μ„œ, κ·Έ 속성듀이 λͺ¨λ‘ `Hashable`을 λ”°λ₯΄λŠ”(conform) ꡬ쑰체

  • `Hashable`을 λ”°λ₯΄λŠ” associated type λ§Œμ„ κ°€μ§€λŠ” μ—΄κ±°ν˜•

  • associated type이 μ—†λŠ” μ—΄κ±°ν˜•

hash(into:) μ—°μ‚°μžλ₯Ό μ œκ³΅λ°›μœΌλ €λ©΄ ν•΄λ‹Ή νƒ€μž…μ— `Hashable` 채택 선언을 ν•˜λ©΄λ©λ‹ˆλ‹€.

 

hash(into:)의 κ΅¬ν˜„μ€ λ”°λ‘œ ν•„μš” μ—†μŠ΅λ‹ˆλ‹€.

 


 

μŠ€μœ„ν”„νŠΈλŠ” `raw value`λ₯Ό 가지지 μ•Šμ€ μ—΄κ±°ν˜•μ— `Comparable` 에 λŒ€ν•œ ν†΅ν•©λœ κ΅¬ν˜„μ„ μ œκ³΅ν•©λ‹ˆλ‹€.

 

μ—΄κ±°ν˜•μ΄ `associated type`을 가지고 μžˆλ‹€λ©΄ κ·Έ νƒ€μž…μ€ `Comparable` ν”„λ‘œν† μ½œμ„ λ°˜λ“œμ‹œ 따라야 ν•©λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄  associated type이 `Int`라면, `Int`λŠ” `Comparable`을 λ”°λ₯΄λ―€λ‘œ μ—΄κ±°ν˜•μ€ `Comparable`을 λ”°λ₯΄κ²Œ λ©λ‹ˆλ‹€.

 

`<`  μ—°μ‚°μžλ₯Ό μ œκ³΅λ°›μœΌλ €λ©΄ ν•΄λ‹Ή νƒ€μž…μ— `Comparable` 채택을 ν•΄μ•Ό ν•©λ‹ˆλ‹€.

 

`Comparable` ν”„λ‘œν† μ½œ 에 기본적으둜  κ΅¬ν˜„λ˜μ–΄ 있기 λ•Œλ¬Έμ— λ‚˜λ¨Έμ§€ μ—°μ‚°μž ( <=, >, >= ) 듀도 ν•¨κ»˜ μ œκ³΅λ©λ‹ˆλ‹€.

 

 

 

끝!