<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 12 Apr 2026 03:20:34 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Crispin Bennett - 2013-11-21 20:53:00</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>@Gwynne Aha. So nothing really to puzzle over there.</description><guid isPermaLink="true">bc35bd51ee855be5f7c0aa055789b8f5</guid><pubDate>Thu, 21 Nov 2013 20:53:00 GMT</pubDate></item><item><title>Gwynne Raskind - 2013-11-21 20:49:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>&lt;b&gt;@Crispin Bennett&lt;/b&gt;: Neither; it's because most of those protocols were added to the headers before the convention of adding &amp;lt;NSObject&amp;gt; to protocols came around. In short, it's backwards compatibility; changing it now would break source compatibility and possibly binary compatibility as well.</description><guid isPermaLink="true">c3499a601824453c7c3806a6c88fca0f</guid><pubDate>Thu, 21 Nov 2013 20:49:42 GMT</pubDate></item><item><title>Crispin Bennett - 2013-11-21 20:21:32</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>Why do some of Apple's protocols (eg. NSFastEnumeration) *not* inherit from &lt;code&gt;&amp;lt;NSObject&amp;gt;&lt;/code&gt;?
&lt;br /&gt;
&lt;br /&gt;Two possibilities I've seen canvassed are (1) that Apple anticipates some protocols may be used with classes not themselves using NSObject as a root class, or (2) to differentiate (semantically) between protocols specifying whole application roles (eg. delegate protocols) and those specifying only individual behaviours (eg. being enumerable).</description><guid isPermaLink="true">972a1e740be8cb1ee759981f2012bb23</guid><pubDate>Thu, 21 Nov 2013 20:21:32 GMT</pubDate></item><item><title>Mark Aufflick - 2013-11-04 20:09:37</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>@Arl one answer is that it's a question of declaring intent. Your class doesn't care if someone passes in an object that has any particular inheritance structure, all you care about is whether it can respond to a particular set of messages.</description><guid isPermaLink="true">51c8bcf6fd8288651a35d7067d482667</guid><pubDate>Mon, 04 Nov 2013 20:09:37 GMT</pubDate></item><item><title>Mark Aufflick - 2013-11-04 20:04:38</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>Just thought I'd point out that MAProxy doesn't adopt the NSObject  protocol ;)
&lt;br /&gt;
&lt;br /&gt;&lt;a href="https://github.com/mikeash/MAFuture/blob/master/MAProxy.h"&gt;https://github.com/mikeash/MAFuture/blob/master/MAProxy.h&lt;/a&gt;</description><guid isPermaLink="true">e4cc257df9d1e9caf6947cbe2dce64c6</guid><pubDate>Mon, 04 Nov 2013 20:04:38 GMT</pubDate></item><item><title>Ari Weinstein - 2013-10-29 17:35:47</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>This is a silly question, but I must ask:
&lt;br /&gt;
&lt;br /&gt;Wouldn't this have the same effect, assuming NSObject is the root class?
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;NSObject&amp;lt;MyProtocol&amp;gt; *obj;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;In fact, due to the protocol and class namespaces being different, you could even do:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;typedef NSObject&amp;lt;MyProtocol&amp;gt; MyProtocol;
&lt;br /&gt;MyProtocol *obj;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;Obviously this is a bad idea for readability and probably other reasons, but it does work - you can even send messages the "MyProtocol" class, though obviously they will end up being sent to NSObject itself.
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;I've actually used this method when implementing a UIViewController class-cluster type thing, where I couldn't cleanly implement a common superclass because some of the classes had to be subclasses of other UIViewControllers. For example, one is a subclass of QLPreviewController and yet another is a subclass of PKAddPassesViewController.
&lt;br /&gt;
&lt;br /&gt;So I implemented the class cluster as a category on UIViewController itself, created a protocol for the "subclasses" implement, and then typedef'd UIViewController&amp;lt;DCPushPreviewController&amp;gt; to DCPushPreviewController, and because UIViewController itself adopts DCPushPreviewController, everything works out perfectly, including sending messages to the "DCPushPreviewController" class.
&lt;br /&gt;
&lt;br /&gt;I'd be thrilled to hear if there was a cleaner way to do this.
&lt;br /&gt;</description><guid isPermaLink="true">be721b3b33db9ccbec4dcb2edcafff3d</guid><pubDate>Tue, 29 Oct 2013 17:35:47 GMT</pubDate></item><item><title>BergQuester - 2013-10-29 04:28:31</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>Looking at the GNUStep implementation of NSObject and NSProxy (I was able to find the source of Apple's NSObject on their open source page, but not NSProxy), over half of the methods are exact or near exact duplicates with minor variations (preprocessor macro differences, variable names). The rest either isProxy forward the method onto the 'real' object (e.g. performSelector:, isKindOfClass:, respondsToSelector:, etc).
&lt;br /&gt;
&lt;br /&gt;Granted, this is GNUStep and not Apple's code and it's not that much code. But assuming Apple shares this amount of duplication between the two, it seems odd to me not have a OneTrueRootClass.
&lt;br /&gt;
&lt;br /&gt;The reason NSProxy doesn't subclass the NSObject class is because, as Mike points out, there's a lot of extra baggage that comes with the class itself. But I'm still failing to see any significant impact in moving &lt;i&gt;just&lt;/i&gt; the NSObject protocol methods to a OneTrueRootClass, aside from historical considerations.</description><guid isPermaLink="true">c8e41fdbefc90d730ede2d1c5487be63</guid><pubDate>Tue, 29 Oct 2013 04:28:31 GMT</pubDate></item><item><title>BergQuester - 2013-10-28 20:44:51</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>KenThomases, inheriting the superclass's (in our case a root class) instance methods is a major reason to subclass. Every instance method declared in the NSObject protocol, except debugDescription, is required. Whether or not these methods were moved to a OneTrueRootClass, NSProxy is required to respond to these methods.
&lt;br /&gt;
&lt;br /&gt;Maybe there's more overhead in consolidating these shared methods than I am aware of, or perhaps the implementation details are so radically different for each of these methods that the code is entirely different. On the surface though, things like retain, release, autorelease, self, perform selector, etc would seem to have a near universal implementation between NSObject and NSProxy.</description><guid isPermaLink="true">c8043f8281f0ca290805d0178bd8985b</guid><pubDate>Mon, 28 Oct 2013 20:44:51 GMT</pubDate></item><item><title>KenThomases - 2013-10-27 05:42:09</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>BergQuester, root classes are special in that class objects also respond to the instance methods of the root class.  Since we want the class objects of subclasses of NSObject to respond to much of the same goodness of NSObject, we'd have to lift that goodness into the root class if NSObject were not the root class.  Then that goodness would also be in NSProxy and defeat the purpose of making NSProxy minimal.
&lt;br /&gt;
&lt;br /&gt;Also, you'd still want the NSObject protocol for the purpose of inheriting into other protocols, as Mike explained.  So you wouldn't gain anything by having a One True Root Class.</description><guid isPermaLink="true">066ecea1808771e63155dbe211f81df5</guid><pubDate>Sun, 27 Oct 2013 05:42:09 GMT</pubDate></item><item><title>Bleyddyn - 2013-10-25 16:26:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>I always inherit from the NSObject protocol because I've yet to run across a case where leaving it out was necessary and I almost always end up wanting to quiet compiler warnings if I don't.
&lt;br /&gt;
&lt;br /&gt;Anyone have any examples where it might be a good idea to leave it off?</description><guid isPermaLink="true">d9514c37281bfaf20895ed3819d063c7</guid><pubDate>Fri, 25 Oct 2013 16:26:25 GMT</pubDate></item><item><title>BergQuester - 2013-10-25 15:52:05</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html#comments</link><description>The mechanics of this make sense. However, why have a protocol to enforce this rather than taking all of the methods in the NSObject protocol and creating the One True Root Class that NSObject, NSProxy, et. al. can inherit from?
&lt;br /&gt;
&lt;br /&gt;Is the choice between a new root class and a protocol just a historical detail?</description><guid isPermaLink="true">6476e6e57ba7015baf678dabae959f72</guid><pubDate>Fri, 25 Oct 2013 15:52:05 GMT</pubDate></item></channel></rss>
