Skip to content

Instantly share code, notes, and snippets.

@adamkuipers
Last active December 9, 2015 21:28
Show Gist options
  • Save adamkuipers/9cbf433f5b70fcd22e39 to your computer and use it in GitHub Desktop.
Save adamkuipers/9cbf433f5b70fcd22e39 to your computer and use it in GitHub Desktop.
Dynamic Dispatch with objc protocols
import Foundation
@objc
protocol Bimpy: NSObjectProtocol {
func bimp() -> String
}
@objc
protocol DefaultBimpy: Bimpy {
func bimp() -> String
}
extension DefaultBimpy {
func bimp() -> String {
return "bimped"
}
}
class Bimper: NSObject, DefaultBimpy {}
func doBimp(bimper: Bimpy) -> String {
return bimper.bimp()
}
doBimp(Bimper())
/*
2015-12-09 13:26:56.976 Streams[50789:949838] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__lldb_expr_54.Bimper bimp]: unrecognized selector sent to instance 0x7fa0a2e0c640'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff93ec0ae2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff828e3f7e objc_exception_throw + 48
2 CoreFoundation 0x00007fff93ec3b9d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff93dfc412 ___forwarding___ + 514
4 CoreFoundation 0x00007fff93dfc188 _CF_forwarding_prep_0 + 120
5 ??? 0x000000010767d56a 0x0 + 4419212650
6 ??? 0x000000010767d0b3 0x0 + 4419211443
7 Streams 0x0000000107022700 main + 0
8 Streams 0x0000000107023b31 _TTRXFo__dT__XFo_iT__iT__ + 17
9 Streams 0x0000000107023861 _TPA__TTRXFo__dT__XFo_iT__iT__ + 81
10 Streams 0x0000000107023bb0 _TTRXFo_iT__iT__XFo__dT__ + 32
11 Streams 0x0000000107023be7 _TTRXFo__dT__XFdCb__dT__ + 39
12 CoreFoundation 0x00007fff93dd04ac __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
13 CoreFoundation 0x00007fff93dc2165 __CFRunLoopDoBlocks + 341
14 CoreFoundation 0x00007fff93dc192e __CFRunLoopRun + 910
15 CoreFoundation 0x00007fff93dc1338 CFRunLoopRunSpecific + 296
16 HIToolbox 0x00007fff8c025935 RunCurrentEventLoopInMode + 235
17 HIToolbox 0x00007fff8c025677 ReceiveNextEventCommon + 184
18 HIToolbox 0x00007fff8c0255af _BlockUntilNextEventMatchingListInModeWithFilter + 71
19 AppKit 0x00007fff8576a0ee _DPSNextEvent + 1067
20 AppKit 0x00007fff85b36943 -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
21 AppKit 0x00007fff8575ffc8 -[NSApplication run] + 682
22 Streams 0x00000001070229b8 main + 696
23 libdyld.dylib 0x00007fff833b45ad start + 1
24 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment