Skip to content

Instantly share code, notes, and snippets.

@SandeepAggarwal
Created July 18, 2021 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SandeepAggarwal/37d0bc6c27ffc44d131d177f7deb7057 to your computer and use it in GitHub Desktop.
Save SandeepAggarwal/37d0bc6c27ffc44d131d177f7deb7057 to your computer and use it in GitHub Desktop.
Demonstrates how an obj-c selector can be replaced in Swift
import Foundation
class A {
func call(selector: Selector, on destination: Any) {
if let object = destination as? NSObject {
object.performSelector(onMainThread: selector, with: self, waitUntilDone: true)
} else {
DispatchQueue.main.async {
Thread.detachNewThreadSelector(selector, toTarget: destination, with: self)
}
}
}
}
class B: NSObject {
@objc func tellMe(sender: Any) {
print("Message received! for B")
}
}
class C {
@objc func tellMe(sender: Any) {
print("Message received! for C")
}
}
let a = A()
let b = B()
let bSelector = #selector(b.tellMe(sender:))
let c = C()
let cSelector = #selector(c.tellMe(sender:))
a.call(selector: bSelector, on: b)
a.call(selector: cSelector, on: c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment