3 ms·
That sounds really screwed up. You can access private methods?
by soperj 11y ago
That sounds really screwed up. You can access private methods?
- draw_down 11y agoWelcome to Objective-C!
- gurkendoktor 11y agoYes, which is useful because it lets us work around bugs in Apple's own frameworks. (There is a trend with Swift to make everything more safe and enterprisey, as in JVM languages, though.) But this isn't about calling private methods, it's about navigating the view hierarchy, which is possible in every GUI framework I've used so far.
- potatolicious 11y agoYeah, but it's pretty much impossible to accidentally do so (as demonstrated in this case). Objective-C has a lot of reflection built in - you can determine an object's class, methods, and even swap out the implementations of methods at runtime. So it's pretty trivial to do something like: if ([someObject respondsToSelector:@selector(_privateMethodName)]) { [someObject performSelector:@selector(_privateMethodName)]; } Apple scans for the easy to detect uses of this (i.e., string literals of private methods in the binary), but of course anyone sufficiently determined to get around it, can. In this case though it's not so much accessing a private method but relying on undocumented and non-guaranteed behavior - walking a view hierarchy looking for a particular view that has no exposed interface. This sort of hackery is highly fragile (it can and does change between releases of iOS), and IMO should not be allowed in a shipping codebase.
- duaneb 11y agoMethod accessibility is enforced statically, but it's still a message send, so you can override the static checks in a number of ways to send the message anyway.
- masklinn 11y agoYou can do so in most languages as long as they're dynamically typed or have a reflection API, and Objective-C is dynamically typed. But in this case private methods aren't exactly involved, the code traverses the views (widgets) hierarchy (which is necessarily public) until it finds an object of a specific class (which comes from a private framework), then it replaces one of the methods to get the behaviour it wants (it actually replaces the class of the object with a subclass overriding that specific method, something many if not most dynamically typed languages allow for, which is way overkill, usually you'd just swap the method — that's called "swizzling" in the objective-c world)
- st3v3r 11y agoYou're not accessing a private method. All UIView subclasses have a method to get the subviews.