Sobes.tech
Junior — Middle

Can you give an example of a pattern similar in function to Observer?

sobes.tech AI

Answer from AI

A pattern similar in function to Observer is NotificationCenter in iOS. It allows objects to subscribe to notifications and receive messages when certain events occur, which is similar to the observer mechanism.

Another example is KVO (Key-Value Observing), which allows tracking changes to object properties.

Example of using NotificationCenter:

NotificationCenter.default.addObserver(self, selector: #selector(handleNotification), name: .myNotification, object: nil)

@objc func handleNotification(_ notification: Notification) {
    // handle notification
}

// Sending a notification
NotificationCenter.default.post(name: .myNotification, object: nil)