A Followup to UIView Explorations

Since posting my article a few days ago about exploring UIView, and how its -removeFromSuperview method is possibly implemented, I've had more thoughts on it, along with some reader feedback, so I'd like to share those now.

First of all, I originally had incorrectly titled the article with “removeFromSubview”, which isn't the correct name, but neither I nor anybody on Programming Reddit / Hacker News seemed to notice (a visitor via reddit eventually did let me know about the mistake). Perhaps I should proof-read a little better!

Secondly, I've been thinking a bit about my recommendation to explore open source “implementations” of AppKit or other frameworks similar to those Apple publishes. One thing I didn't mention in my original article, but it's good advice, if you're poking around at these implementations, keep in mind this might complicate things if you ever decided to work for Apple. I have no idea what their legal policy is, but I know for many companies, looking at open source implementations of something similar to what you'll be working on can make you “dirty”, meaning since you've seen open source implementations, you could potentially be contaminating their proprietary implementations, which could potentially be a liability. Again, I don't know if this is Apple's policy, or if something like Cocotron would even constitute such a violation, but it would be prudent to keep this in mind.

Finally, I've had some comments from readers about my “guess” implementation of UIView's methods, where I've really simplified it a bit too much. As UIView is really just a lightweight wrapper around Core Animation layers, it doesn't do much work for the view hierarchy on its own. CALayer is in charge of managing the tree. This implementation is quite different than [my understanding of] NSView's implementation, although the public-facing API remains similar.

Another reader commented that UIView might use a private method internally like -_removeSubview:, instead of my more convoluted approach of getting the mutable subviews array, and removing the view from that list. When I think about it this way, it makes a lot more sense. The lesson is the same (instances of the same class can invoke private methods on other instances), but the result is much cleaner, and probably more likely to be correct.

Of course, if I really wanted to verify, there's nothing stopping me from setting some breakpoints and stepping through code, or using classdump to see what UIView is made of. I'll leave that as an exercise for the reader.

Speed of Light