<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Apfelsaft &#187; initialize</title>
	<atom:link href="http://www.deelen.de/tag/initialize/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deelen.de</link>
	<description>Yet another Mac developer blog</description>
	<lastBuildDate>Sun, 03 Mar 2024 08:13:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>+ (void)initialize called only once?</title>
		<link>http://www.deelen.de/2010/02/voidinitialize-called-only-once/</link>
		<comments>http://www.deelen.de/2010/02/voidinitialize-called-only-once/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 15:20:02 +0000</pubDate>
		<dc:creator>Joachim</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[bindings]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[initialize]]></category>
		<category><![CDATA[objectivec]]></category>
		<category><![CDATA[subclass]]></category>

		<guid isPermaLink="false">http://www.deelen.de/?p=65</guid>
		<description><![CDATA[To make a long story short: The answer is no!
Even if you don&#8217;t subclass, there are side effects you&#8217;d never thought possible.

A few days ago I ran into a tricky problem while working on my iTunes-Controller Playwatch. After spending some time debugging I came to the conclusion that only multiple calls to the &#8220;initialize-method&#8221; could [...]]]></description>
			<content:encoded><![CDATA[<p>To make a long story short: The answer is no!</p>
<p>Even if <strong>you</strong> don&#8217;t subclass, there are side effects you&#8217;d never thought possible.<br />
<span id="more-65"></span></p>
<p>A few days ago I ran into a tricky problem while working on my iTunes-Controller Playwatch. After spending some time debugging I came to the conclusion that only multiple calls to the &#8220;initialize-method&#8221; could cause the problem.</p>
<p>Apple’s documentation states, that initialize is called only once. If you create subclasses of your class, initialize gets called for each subclass. If the subclass does not implement it, the call is forwarded to the superclass. This is the exception to the rule. If you subclass, initialize of your superclass maybe called more than once. To avoid double initialization, the documentation recommends to check the class-instance and do initialization only if it’s the correct one.</p>
<p>Here&#8217;s the code:</p>
<pre class="brush: objc;">
@implementation MyClass
+ (void)initialize
{
	if(self == [MyClass class]) {
		// Do initialization here....
	}
}
@end
</pre>
<p>Since I did not have any subclasses, I thought I could run without this check. Far wrong! To find out what was going on, I set a breakpoint at initialize. The debugger stopped once, and a second time. Even with no subclasses. How could that be? A look at the Variable “self” shed some light on this miracle.</p>
<p>The first time the stack and the variable looked like this: (Click on the images to see full resolution)</p>
<p><a href="http://www.deelen.de/wordpress/wp-content/uploads/2010/02/Bildschirmfoto-2010-02-15-um-14.56.20.png"><img src="http://www.deelen.de/wordpress/wp-content/uploads/2010/02/Bildschirmfoto-2010-02-15-um-14.56.20.png" alt="" title="initialized called the first time" width="542" height="330" class="alignnone size-full wp-image-83" /></a></p>
<p>The second time the result was as follows:</p>
<p><a href="http://www.deelen.de/wordpress/wp-content/uploads/2010/02/Bildschirmfoto-2010-02-15-um-14.57.04.png"><img src="http://www.deelen.de/wordpress/wp-content/uploads/2010/02/Bildschirmfoto-2010-02-15-um-14.57.04.png" alt="" title="initialize called a second time" width="556" height="328" class="alignnone size-full wp-image-84" /></a></p>
<p>The first call to initialize is on the class “PWCoverArtAndTracks”. The second one is on the class “NSKVONotifying_PWCoverArtAndTracks”.</p>
<p>All right! The AppKit did create a subclass because I use the tracks-array of “PWCoverArtAndTracks” as the content binding for a NSCollectionView. As you can see at the picture below.</p>
<p><a href="http://www.deelen.de/wordpress/wp-content/uploads/2010/02/Bildschirmfoto-2010-02-15-um-15.18.45.png"><img src="http://www.deelen.de/wordpress/wp-content/uploads/2010/02/Bildschirmfoto-2010-02-15-um-15.18.45.png" alt="" title="Class used for binding in IB" width="968" height="478" class="alignnone size-full wp-image-64" /></a></p>
<p>This is an implementation detail of CocoaBindings. I can remember reading something about this detail in the Apple Docs. But that remembrance was almost vanished at the time I wrote the initialize method.</p>
<p>Here’s my advice: Always check the class within initialize. Even if you don’t have subclasses.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deelen.de/2010/02/voidinitialize-called-only-once/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
