Skip to content

Instantly share code, notes, and snippets.

@casademora
Last active February 25, 2019 21:22
Show Gist options
  • Save casademora/753834752f3cb43b9b44 to your computer and use it in GitHub Desktop.
Save casademora/753834752f3cb43b9b44 to your computer and use it in GitHub Desktop.
Private func in swift are hidden from the ObjC runtime
import Foundation
class Testing : NSObject
{
private func privateWork() {
}
internal func internalWork(){
}
func check(){
self.respondsToSelector(Selector("privateWork")) //false
self.respondsToSelector(Selector("internalWork")) //true
}
}
let t = Testing()
t.check()
@ryanmasondavies
Copy link

Huh. That's neat. What about 'private' Objective-C selectors? Are they visible? (I would guess yes.)

@oleganza
Copy link

oleganza commented Jan 4, 2015

@iotize in runtime all obj-c selectors are "public".

@casademora: could it be that Swift simply inlined that func, noticed it's never used anywhere and therefore created no selector for it? So some more complex private function would show up in respondsToSelector?

@merriam
Copy link

merriam commented Jan 4, 2015

@oleganza: I think Swift reserves the right to inline the private func. Much like the old JVM, it reserves the right to do lots of optimizations by enforcing rules.

@KiGi
Copy link

KiGi commented Jan 4, 2015

@objc private func privateWork() - Now you get true//true, because it had to make a dynamically dispatched version which performSelector can see... just something to keep in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment