<?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>OmegaDelta &#187; objective-c</title>
	<atom:link href="http://omegadelta.net/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://omegadelta.net</link>
	<description>OmegaDelta</description>
	<lastBuildDate>Thu, 09 Sep 2010 13:30:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Encode what cannot be NSCoded</title>
		<link>http://omegadelta.net/2010/05/11/encode-what-cannot-be-nscoded/</link>
		<comments>http://omegadelta.net/2010/05/11/encode-what-cannot-be-nscoded/#comments</comments>
		<pubDate>Tue, 11 May 2010 13:18:13 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[iphone dev]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://omegadelta.net/?p=1674</guid>
		<description><![CDATA[Isn&#8217;t it annoying that UIImage can&#8217;t be serialized (as evident with the error &#8220;UIImage unrecognized encodeWithCoder&#8221;).  Really throws a spanner in the works when trying to serialise your UI (or applying this hack) given that images are everywhere&#8230; Well this can easily be fixed with a monkey-patch, and a pretty safe one at that. Here [...]]]></description>
			<content:encoded><![CDATA[<p>Isn&#8217;t it annoying that UIImage can&#8217;t be serialized (as evident with the error &#8220;UIImage unrecognized encodeWithCoder&#8221;).  Really throws a spanner in the works when trying to serialise your UI (or applying <a href="http://omegadelta.net/2010/05/11/copy-what-cannot-be-nscopied/">this hack</a>) given that images are everywhere&#8230;</p>
<p>Well this can easily be fixed with a monkey-patch, and a pretty safe one at that.</p>
<p>Here it is (just throw this in a &#8220;.m&#8221; file, and include it in your project):</p>
<pre class="brush:objc">@implementation UIImage (NSCoder)

- (id) initWithCoder:(NSCoder *)coder {

	self = [self initWithData:[coder decodeObjectForKey:@"PNGdata"]];

	return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder {

	// if space is a concern, replace with UIImageJPEGRepresentation

	[encoder encodeObject:UIImagePNGRepresentation(self) forKey:@"PNGdata"];
}

@end</pre>
<p><span style="font-size: 13.3333px;">So there you go!  This hack was brought to you by the people behind <a href="http://gpslog.cc/">GPS Log for iPhone</a> (please check it out, it&#8217;s free to try!).</span></p>
<p>Why do I claim that this monkey-patch is safe?  Well it encodes and decodes your image. The method is doing what it is contracted to do.  *If* UIImage were to implement this method, then you would have issues, but *only* if you serialized with one implementation, and deserialized with another (which wouldn&#8217;t normally happen, and it&#8217;s a pretty big *if* anyway&#8230;).</p>
<p>Why is it a &#8220;monkey patch&#8221;?  Well you&#8217;re throwing your own code into UIImage implementing standard methods&#8230;  pretty much the text-book definition.</p>
<p>Be aware&#8230; serialising images to PNG format will consume a decent amount of memory and disk space, and can be slow, especially if those images are big.  So if you already have these images on disk somewhere else, then you probably should be loading them from disk rather than doing this (I have a feeling this is why Apple did not implement this method, to force people to load from disk).</p>
<p>So why is this method useful?  Well for one, you can use it to <a href="http://omegadelta.net/2010/05/11/copy-what-cannot-be-nscopied/">Copy what cannot be NSCopied</a> :)</p>
]]></content:encoded>
			<wfw:commentRss>http://omegadelta.net/2010/05/11/encode-what-cannot-be-nscoded/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copy what cannot be NSCopied</title>
		<link>http://omegadelta.net/2010/05/11/copy-what-cannot-be-nscopied/</link>
		<comments>http://omegadelta.net/2010/05/11/copy-what-cannot-be-nscopied/#comments</comments>
		<pubDate>Tue, 11 May 2010 12:54:50 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[iphone dev]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://omegadelta.net/?p=1665</guid>
		<description><![CDATA[Sometimes you need to deep-copy an object in Objective-C.  Normally you just go newObject = [oldObject copy].  But what if oldObject does not support NSCopying (typified by the error &#8220;copyWithZone: unrecognized selector&#8221;)? Well.. if your object supports NSCoding, then you can simply serialize it, and deserialize it!  Saves you having to monkey-patch the copyWithZone method [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to deep-copy an object in Objective-C.  Normally you just go newObject = [oldObject copy].  But what if oldObject does not support NSCopying (typified by the error &#8220;copyWithZone: unrecognized selector&#8221;)?</p>
<p>Well.. if your object supports NSCoding, then you can simply serialize it, and deserialize it!  Saves you having to monkey-patch the copyWithZone method (though may be a tiny bit less efficient at runtime).</p>
<p>How can do you perform this voodoo magic?  just one line of code&#8230;</p>
<pre class="brush:objc">newObject = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:oldObject]];</pre>
<p>NB. (for iPhone programmers) &#8216;newObject&#8217; is autoreleased, so make sure you retain it somewhere!</p>
<p>There you go.  This hack was brought to you by the people behind <a href="http://gpslog.cc/">GPS Log for iPhone</a> (please check it out, it&#8217;s free to try!).</p>
<p>Where do I use this?  Well I have some UITableViewCell objects that I defined in an XIB file, but I need lots of copies (this is a pretty common use-case, which the SDK does not handle very well at all).  Instead of sticking the object in it&#8217;s own XIB file, and loading it from the XIB each time (as is one workaround), I use the trick above :)  And it&#8217;s more more efficient (object is copied in memory, rather than loaded from flash-storage).</p>
<p>In fact, many UI elements don&#8217;t support copying, but they do support serializing, so this can be pretty useful.  For UI elements, you may need to add <a href="http://omegadelta.net/2010/05/11/encode-what-cannot-be-nscoded/">this monkey-patch</a> to get UIImage to play nice.</p>
]]></content:encoded>
			<wfw:commentRss>http://omegadelta.net/2010/05/11/copy-what-cannot-be-nscopied/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-C: NSArray of weak references</title>
		<link>http://omegadelta.net/2010/03/08/objective-c-nsarray-of-weak-references/</link>
		<comments>http://omegadelta.net/2010/03/08/objective-c-nsarray-of-weak-references/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 02:04:35 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://omegadelta.net/?p=1587</guid>
		<description><![CDATA[Need an array of weak references? e.g. for delegates. Here&#8217;s some good tips. One convenient way is to box it in a NSValue with a non-retained object. NSValue *value = [NSValue valueWithNonretainedObjectValue:myObj]; [array addObject:value]; and when you get the object: value = [array objectAtIndex:x]; myObj = [value nonretainedObjectValue];]]></description>
			<content:encoded><![CDATA[<p>Need an array of weak references?  e.g. for delegates.  Here&#8217;s some <a href="http://www.omnigroup.com/mailman/archive/macosx-dev/2004-August/053984.html"><span style="color: #000000;">good tips</span></a>.</p>
<p>One convenient way is to box it in a NSValue with a non-retained object.</p>
<pre>NSValue *value = [NSValue valueWithNonretainedObjectValue:myObj];
[array addObject:value];

and when you get the object:

value = [array objectAtIndex:x];
myObj = [value nonretainedObjectValue];</pre>
]]></content:encoded>
			<wfw:commentRss>http://omegadelta.net/2010/03/08/objective-c-nsarray-of-weak-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objective-C tip of the day</title>
		<link>http://omegadelta.net/2009/02/21/objective-c-tip-of-the-day/</link>
		<comments>http://omegadelta.net/2009/02/21/objective-c-tip-of-the-day/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 07:53:00 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[iphone dev]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://omegadelta.net/2009/02/21/objective-c-tip-of-the-day/</guid>
		<description><![CDATA[Don&#8217;t ignore warnings. If you get a warning it&#8217;s probably actually an error &#8211; things like misspelt methods (i.e. messages) which would be classed as an error in C++/Java/C# are just warnings due to the dynamic nature of Objective-C &#8211; and yet are just as bad (your method isn&#8217;t being called!) So fix all your [...]]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t ignore warnings.  If you get a warning it&#8217;s probably actually an error &#8211; things like misspelt methods (i.e. messages) which would be classed as an error in C++/Java/C# are just warnings due to the dynamic nature of Objective-C &#8211; and yet are just as bad (your method isn&#8217;t being called!)</p>
<p>So fix all your warnings &#8211; if the warning actually doesn&#8217;t matter &#8211; then take steps to remove it anyway so your bad warnings don&#8217;t get lost in the mix.</p>
]]></content:encoded>
			<wfw:commentRss>http://omegadelta.net/2009/02/21/objective-c-tip-of-the-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>objective-c dynamic invocation</title>
		<link>http://omegadelta.net/2009/02/11/objective-c-dynamic-invocation/</link>
		<comments>http://omegadelta.net/2009/02/11/objective-c-dynamic-invocation/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 13:07:23 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[iphone dev]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://omegadelta.net/2009/02/11/objective-c-dynamic-invocation/</guid>
		<description><![CDATA[Useful stuff to know: dynamic invocation in Objective C.]]></description>
			<content:encoded><![CDATA[<p>Useful stuff to know: <a href="http://theocacao.com/document.page/264">dynamic invocation in Objective C</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://omegadelta.net/2009/02/11/objective-c-dynamic-invocation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
