ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [iOS] Equatable μ΄λž€?
    iOS/🍎 Documentation 2021. 1. 20. 16:57

    Equatable μ€ κ°’μ˜ 동등성 λΉ„ꡐ κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” νƒ€μž…μž…λ‹ˆλ‹€.

    μ—₯? ν”„λ‘œν† μ½œμΈλ° μ™œ νƒ€μž…μ΄λƒ? 

    μŠ€μœ„ν”„νŠΈμ—μ„œλŠ” ν”„λ‘œν† μ½œλ„ νƒ€μž…μž…λ‹ˆλ‹€! 😲

    κ΄€λ ¨ κΈ€: Protocol as Types

     

     

    μŠ€μœ„ν”„νŠΈμ—μ„œ κ°’μ˜ κ°™μŒμ€ == (equal to) 와 != (not equal to) μ—°μ‚°μžλ‘œ λΉ„κ΅ν•©λ‹ˆλ‹€.

     

    Equatable ν”„λ‘œν† μ½œμ„ μ±„νƒν•œ νƒ€μž…μ€ == 와 != μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

     

    Swift의 primitive type 듀은 Equatable 을 μžλ™μœΌλ‘œ μ§€μ›ν•©λ‹ˆλ‹€. (Int, Double, String ... λ“±)

     

    κ·Έλ ‡λ‹€λ©΄ μ»€μŠ€ν…€ νƒ€μž…μΈ enum, struct, class 의 κ²½μš°λŠ” μ–΄λ–¨κΉŒμš”?

     

    ν•˜λ‚˜μ”© λ³΄κ² μŠ΅λ‹ˆλ‹€.πŸ‘‡πŸ»πŸ‘‡πŸ»πŸ‘‡πŸ»


     

    enum

    κ΄€λ ¨ κΈ€ : emumeration 총정리

     

    μ—°κ΄€ 값이 μ—†λŠ” κΈ°λ³Έ μ—΄κ±°ν˜• ν˜•νƒœμΌ 경우 Equtable ν”„λ‘œν† μ½œ conformance(채택) λ₯Ό μ„ μ–Έν•˜μ§€ μ•Šμ•„λ„ μžλ™μœΌλ‘œ μ§€μ›ν•©λ‹ˆλ‹€.

    enum Color {
    	case red,green,blue
    }
    
    var foregroundColor = Color.red
    var backgroundColro = Color.blue
    
    forgroundColor == backgroundColor // false

     

    μ—°κ΄€ 값이 μžˆλŠ” 경우, μ—°κ΄€ κ°’μ˜ νƒ€μž…μ΄ Equatable 을 μ±„νƒν•œ 경우 ν”„λ‘œν† μ½œ conformance μ„ μ–Έλ§Œ ν•΄μ£Όλ©΄ μžλ™ μ§€μ›ν•©λ‹ˆλ‹€.

    그렇지 μ•Šμ€ 경우 직접 == κ³Ό !=  μ—°μ‚°μžλ₯Ό κ΅¬ν˜„ν•΄μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.

     

    " μ—°μ‚°μžλ„ ν•¨μˆ˜μž…λ‹ˆλ‹€ "

    μœ„ ν† ν”½κ³ΌλŠ” 관련이 μ—†μ§€λ§Œ, ν•¨μˆ˜λŠ” 일급 객체 μ΄λ―€λ‘œ λ³€μˆ˜μ— μ €μž₯, νŒŒλΌλ―Έν„°λ‘œ 전달, 리턴 κ°’μœΌλ‘œ μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€.

    λŒ€ν‘œμ μΈ κ³ μ°¨ν•¨μˆ˜μΈ map, filter, reduce λ“±μ˜ λ‘λ²ˆμ§Έ νŒŒλΌλ―Έν„°μ—  '+', '-' 와 같은 μ—°μ‚°μžλ§Œ 전달할 수 μžˆλŠ” μ΄μœ λŠ” μ—°μ‚°μžλ„ ν•¨μˆ˜μ΄κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.


     

    struct

    μ €μž₯ 속성이 Equatable을 μ±„νƒν•œ νƒ€μž…μœΌλ‘œ 이루어져 있으면 ν”„λ‘œν† μ½œ conformance μ„ μ–Έλ§Œ ν•΄μ£Όλ©΄ μžλ™ μ§€μ›ν•©λ‹ˆλ‹€.

     

    그렇지 μ•Šμ€ 경우 직접 == κ³Ό !=  μ—°μ‚°μžλ₯Ό κ΅¬ν˜„ ν•΄μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€. 

    struct Point: Equatable {
        var x: Double
        var y: Double 
    }
    
    var x = Point(x:1, y:2)
    var y = Point(x:2, y:3)
    
    x == y // false
    // μžλ™ μ œκ³΅λ˜μ§€λ§Œ 직접 κ΅¬ν˜„ν•΄μ€€λ‹€λ©΄ μ΄λŸ°μ‹μ˜ μ½”λ“œκ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€.
    // '==' μ—°μ‚°μžλ§Œ κ΅¬ν˜„ν•˜λ©΄ '!=' μ—°μ‚°μžλŠ” μžλ™μ œκ³΅λ©λ‹ˆλ‹€.
    extension Point {
          static func ==(lhs: Point, rhs: Point) -> Bool {
              return (lhs.x == rhs.x) && (lhs.y == rhs.y)
         }
    }

     


     

    class

    κ΅¬ν˜„μ΄ μžλ™ μ œκ³΅λ˜μ§€ μ•ŠμœΌλ―€λ‘œ, ν”„λ‘œν† μ½œ conformance λ₯Ό μ„ μ–Έν•˜κ³  μ—°μ‚°μžλ₯Ό 직접 κ΅¬ν˜„ν•΄μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.

    class StreetAddress {
        let number: String
        let street: String
        let unit: String?
    
        init(_ number: String, _ street: String, unit: String? = nil) {
            self.number = number
            self.street = street
            self.unit = unit
        }
    }
    
    // '==' μ—°μ‚°μžλ₯Ό 직접 κ΅¬ν˜„ν•΄μ£Όμ–΄μ•Όν•œλ‹€. == 이 κ΅¬ν˜„λ˜λ©΄ != λŠ” μžλ™ μ œκ³΅λœλ‹€.
    extension StreetAddress: Equatable {
        static func == (lhs: StreetAddress, rhs: StreetAddress) -> Bool {
            return
                lhs.number == rhs.number &&
                lhs.street == rhs.street &&
                lhs.unit == rhs.unit
        }
    }

     

    Equality is Separate From Identity

     

    Equatable은 값이 같은지 νŒλ‹¨ν•˜λŠ” μ—°μ‚°μžμ™€ κ΄€λ ¨λœ ν”„λ‘œν† μ½œμž…λ‹ˆλ‹€.

    '==' 와 '!='

     

    같은 μΈμŠ€ν„΄μŠ€μž„μ„ νŒλ‹¨ν•˜λŠ” μ—°μ‚°μžκ°€ μ•„λ‹™λ‹ˆλ‹€.

     

    같은 μΈμŠ€ν„΄μŠ€μž„μ„ νŒλ‹¨ν•  λ•ŒλŠ” '===' μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

     

     

     

    끝!


     

    λŒ“κΈ€

μ–΄μ œλ³΄λ‹€ λ°œμ „ν•œ λ‚˜