Skip to content

Instantly share code, notes, and snippets.

View avdwerff's full-sized avatar

Alexander van der Werff avdwerff

View GitHub Profile
extension Array where Element: Comparable {
func mergeSort() -> [Element] {
if count <= 1 { return self }
else {
let left = Array(self[0 ..< count/2])
let right = Array(self[count/2 ..< count])
return merge(left.mergeSort(), right: right.mergeSort())
}
}
@hollance
hollance / Explanation.md
Last active September 25, 2017 03:35
Communicate between objects using channels

Communicate between objects using channels

When you have two objects A and B, say two view controllers, that you want to have talk to each other, you can choose from the following options:

  • NSNotificationCenter. This is anonymous one-to-many communication. Object A posts a notification to the NSNotificationCenter, which then distributes it to any other objects listening for that notification, including Object B. A and B do not have to know anything about each other, so this is a very loose coupling. Maybe a little too loose...

  • KVO (Key-Value Observing). One object observes the properties of another. This is a very tight coupling, because Object B is now peeking directly into Object A. The advantage of KVO is that Object A doesn't have to be aware of this at all, and therefore does not need to send out any notifications -- the KVO mechanism takes care of this behind the scenes.

  • Direct pointers. Object A has a pointer to Object B and directly sends it messages when something of interest h