[Behavioral Patterns] - Observer

이 글은 refactoring guru의 - Observer 부분을 읽고 번역 및 정리한 글입니다.

velog.io

위 블로그를 보고 따라함

 

 

 

 

 

 

공부하면서..

 

Subject 클래스를 작성할 때,

Observer protocol 타입의 배열을 선언하고

메소드의 매개변수로 Observer protocol 타입을 받음

이후 배열에 매개변수로 받아온  Observer가 있는지 contains 하니 아래와같은 오류 남

error: Cannot convert value of type 'any Observer' to expected argument type '(any Observer) throws -> Bool'

 

냅다 equal 갈겨보니 오류남. protocol type은 비교할수 없나봄?

error: Type 'any Observer' cannot conform to 'Equatable'

 

그래서 그냥 id 변수를 만들고 Observer protocol 상속하는 클래스 이름을 넣어서 id값만 비교했다

 

 

 

 

그리고 블로거가 공부하는 사이트 들어가보니 스위프트 언어도 지원중... 예.. 나도 이용해야지.,,

 

 

Observer in Swift / Design Patterns

Usage examples: The Observer pattern is pretty common in Swift code, especially in the GUI components. It provides a way to react to events happening in other objects without coupling to their classes. Identification: The pattern can be recognized by subsc

refactoring.guru

 

+ Recent posts