<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2010-08-12-implementing-nscoding.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 10 May 2026 05:30:04 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>mikeash - 2010-08-16 03:05:27</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>Good point on possibly overwriting instance variables. I suppose if you use &lt;code&gt;[self init]&lt;/code&gt; or similar, you shouldn't be replacing instance variables directly.
&lt;br /&gt;
&lt;br /&gt;For implementing &lt;code&gt;NSCoding&lt;/code&gt;, I think the best approach is to decode the values you need, then call through to the "real" initializer.
&lt;br /&gt;
&lt;br /&gt;Incidentally, I think &lt;code&gt;NSView&lt;/code&gt;'s approach of having two separate designated initializers is painful and really shouldn't have been built that way. If &lt;code&gt;initWithCoder:&lt;/code&gt; would call through to &lt;code&gt;initWithFrame:&lt;/code&gt;, it would avoid a decent bit of hassle involved in subclassing Cocoa views. Too late to change it now, I imagine.
&lt;br /&gt;
&lt;br /&gt;While it works to have multiple designated initializers (so long as only one ever runs), I think it's best to try to only ever have one.</description><guid isPermaLink="true">baf46ccdfce03217dfb5c7d156f9a957</guid><pubDate>Mon, 16 Aug 2010 03:05:27 GMT</pubDate></item><item><title>Ken Ferry - 2010-08-16 02:28:24</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>The call to [self init] from -initWithCoder: could probably use some more discussion - I would have written [super init] there.  
&lt;br /&gt;
&lt;br /&gt;Done as you have here, -initWithCoder: is not a designated initializer of the class.  -initWithCoder: is a designated initializer of say, NSView, so this is a pitfall.  If the -init method of this class or any subclass  writes to any of the ivars, code in -initWithCoder: method of this class or one of its subclasses is likely to overwrite the ivars and leak.  
&lt;br /&gt;
&lt;br /&gt;I thought you had a post on designated initializers, but maybe not.  In any case, the goal is to make this rule true: One and only one of each class's designated initializes runs during the bringup of an object.  Each _class_, not object, has a set of designated initializers.  The sets of designated initializers of a class and of its superclass are not necessarily related.  </description><guid isPermaLink="true">d2b9e449011067103b81cafb2ba00575</guid><pubDate>Mon, 16 Aug 2010 02:28:24 GMT</pubDate></item><item><title>YT - 2010-08-14 16:14:55</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>Thank you for your answer! As always, it's a great article.</description><guid isPermaLink="true">0ae6cfcd20d972e7cb0ee77ce5f859f5</guid><pubDate>Sat, 14 Aug 2010 16:14:55 GMT</pubDate></item><item><title>mikeash - 2010-08-14 15:34:32</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>Well, &lt;code&gt;int&lt;/code&gt; is 32-bit even on 64-bit Mac OS X, so the only problem there is endianness. &lt;code&gt;NSCoder&lt;/code&gt; knows about endianness and will take care of any byte flipping.
&lt;br /&gt;
&lt;br /&gt;For a type of inconsistent size, such as &lt;code&gt;NSInteger&lt;/code&gt;, it will also take care of adjusting the size. However, if you use such a size, you must ensure that you never store a value on the 64-bit machine that can't be represented on the 32-bit machine, otherwise you'll get an exception when decoding.</description><guid isPermaLink="true">827e0838ec86518a65f36e785137cf81</guid><pubDate>Sat, 14 Aug 2010 15:34:32 GMT</pubDate></item><item><title>YT - 2010-08-14 06:02:18</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>What happens if you encodeInt:forKey: on a 64 bit ppc machine, and then decodeInt:forKey:  the resulting archive on a 32 bit intel machine, say? </description><guid isPermaLink="true">85d11d84f576765b24b09809870d3477</guid><pubDate>Sat, 14 Aug 2010 06:02:18 GMT</pubDate></item><item><title>jt - 2010-08-14 01:32:57</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>Thanks once again, Mike, for taking the time to bring us another terrific instalment.</description><guid isPermaLink="true">d832ac108502551f803df94929ecfc9c</guid><pubDate>Sat, 14 Aug 2010 01:32:57 GMT</pubDate></item><item><title>mikeash - 2010-08-13 22:57:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>I'd really wonder at any code that had to serialize and deserialize a view controller. Typically you should only be serializing model objects. (Nibs are obviously a big exception to this, but of course that's already done for you.) What was your use case for this?
&lt;br /&gt;
&lt;br /&gt;Anyway, that's the sort of advice which isn't completely universal, but by the time you need to violate it you'll know enough to do so, so it isn't necessary to spell out all of the caveats.</description><guid isPermaLink="true">ec366998fb5e6462d7a4da807a08efa8</guid><pubDate>Fri, 13 Aug 2010 22:57:26 GMT</pubDate></item><item><title>Farcaller - 2010-08-13 20:49:18</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>&amp;gt; You must call the superclass versions if the superclass implements NSCoding
&lt;br /&gt;
&lt;br /&gt;Actually no. A sample is iOS'es UIViewContoller that implements its view serialization as a part of encodeWithCoder:. I had a case when I need to serialize the UIViewController subclass that creates its view in runtime, so I had to override encodeWithCoder: with own version that does not call the superclass. This way it would be deserealized with view==nil and could re-create the view according to new UI conditions.</description><guid isPermaLink="true">ddc23a7af58deeb9da689c34812ee357</guid><pubDate>Fri, 13 Aug 2010 20:49:18 GMT</pubDate></item><item><title>Peter - 2010-08-13 18:55:41</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>Just a heads-up: I've noticed that specifying a different name for a class causes a crash in iOS 3.2.</description><guid isPermaLink="true">6644ce8eab5a19017462c2a0d897993e</guid><pubDate>Fri, 13 Aug 2010 18:55:41 GMT</pubDate></item><item><title>mikeash - 2010-08-13 17:14:44</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>You must call the superclass versions if the superclass implements &lt;code&gt;NSCoding&lt;/code&gt;, but not if it doesn't. That's the case if you subclass &lt;code&gt;NSObject&lt;/code&gt;, but also any one of the numerous other classes that don't implement it.</description><guid isPermaLink="true">ec0cd5c9600f854f802d6a70a87c2f8e</guid><pubDate>Fri, 13 Aug 2010 17:14:44 GMT</pubDate></item><item><title>Jose Vazquez - 2010-08-13 17:05:07</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2010-08-12-implementing-nscoding.html#comments</link><description>Thanks for the post. This article would have saved me some headaches a few weeks ago. 
&lt;br /&gt;
&lt;br /&gt;I do have one more related question. From what I understood in the Hillegass book, you should call encodeWithEncoder: and initWithEncoder: on super if you are subclassing something other than NSObject. You didn't mention that in your article, so I wonder if you disagree or simply forgot to mention it.
&lt;br /&gt;
&lt;br /&gt;Thanks again for the article. </description><guid isPermaLink="true">e1bad8d6af8b1719259159cdce3680a3</guid><pubDate>Fri, 13 Aug 2010 17:05:07 GMT</pubDate></item></channel></rss>
