Skip to content

Instantly share code, notes, and snippets.

View MartinLau7's full-sized avatar
🌴
On vacation

MartinLau MartinLau7

🌴
On vacation
  • Macau
  • Macau
  • 10:26 (UTC +08:00)
View GitHub Profile
@MartinLau7
MartinLau7 / Implement Events in Swift
Created February 10, 2023 08:53 — forked from jonasalbert/Implement Events in Swift
Implement Events in Swift
http://blog.scottlogic.com/2015/02/05/swift-events.html
I initially started writing a blog post that discussed various alternatives to Key-Value-Observing (KVO) in Swift, however, I found myself writing more about the task of implementing events in Swift. So, I’ve decided to split the problem in two, in this post I’ll cover events, and I’ll follow-up with the KVO post later on.
I’m certainly not the first to blog about this topic, Mike Ash published a great article on Swift implementation of NSNotification just last week. However, in this post I’d like to approach the problem in a slightly different way, starting with a naïve implementation.
Let’s start at the beginning …
WHAT ARE EVENTS?
Cocoa has a number of techniques that allow classes to collaborate in a loosed-coupled fashion via some form of notification, including target-action, the delegate pattern, NSNotification and KVO. These are all forms of the classic Observer Pattern, yet all are different implementations, and each have their own failings.
//
// AKNativeAnisetteService.m
// akd
//
// Created by Scott Knight on 5/10/19.
// Copyright © 2019 Scott Knight. All rights reserved.
//
#import <AuthKit/AuthKit.h>
#import "AKNativeAnisetteService.h"