[swift] 46. Equatable

RudinP·2023년 10월 19일
0

Study

목록 보기
65/227

Equatable

  • 객체가 동일한지(not 주소) 확인할 수 있도록 하는 프로토콜 (임베디드 되어있음)
  • 비교연산자를 만드는 느낌
  • lhs rhs 두개의 필드로 구별
struct Pet : Equatable{
	let id: String
    let name: String
    
    // != 로 작성해도 ok
    static func == (lhs: Pet, rhs: Pet) -> Bool {
    	return lhs.id == rhs.id
    }
    
}

let myPet1 = Pet(id: "01", name: "고양이")
let myPet2 = Pet(id: "02", name: "댕댕이")
let myPet3 = Pet(id: "03", name: "개냥이")

if myPet1 == myPet3 {
	print("두 펫은 같다")
}
//두 펫은 같다 출력
profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글