<?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>Codec &#187; Objective-C</title>
	<atom:link href="http://www.trembl.org/codec/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.trembl.org/codec</link>
	<description>A Personal Polylogic Code/Decode &#039;Zettelkasten&#039;</description>
	<lastBuildDate>Fri, 10 Sep 2010 09:12:16 +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>Layering one UIImage onto of another UIImage</title>
		<link>http://www.trembl.org/codec/569/</link>
		<comments>http://www.trembl.org/codec/569/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 08:15:45 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Categories]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[alpha]]></category>
		<category><![CDATA[Category]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[overlay]]></category>
		<category><![CDATA[UIImage]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/569/</guid>
		<description><![CDATA[Combining two images, especially useful, if the overlay image has an alpha value: // // UIImage+Category.h // ImageOverlay // // Created by Georg Tremmel on 29/04/2010. // &#160; #import &#60;Foundation/Foundation.h&#62; &#160; &#160; @interface UIImage &#40;combine&#41; &#160; - &#40;UIImage*&#41;overlayWith:&#40;UIImage*&#41;overlayImage; &#160; @end And the implementation file. // // UIImage+Category.m // ImageOverlay // // Created by Georg Tremmel [...]]]></description>
			<content:encoded><![CDATA[<p>Combining two images, especially useful, if the overlay image has an alpha value:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  UIImage+Category.h</span>
<span style="color: #11740a; font-style: italic;">//  ImageOverlay</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Georg Tremmel on 29/04/2010.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> UIImage <span style="color: #002200;">&#40;</span>combine<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>overlayWith<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>overlayImage;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>And the implementation file.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  UIImage+Category.m</span>
<span style="color: #11740a; font-style: italic;">//  ImageOverlay</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Georg Tremmel on 29/04/2010.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;UIImage+Category.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> UIImage <span style="color: #002200;">&#40;</span>combine<span style="color: #002200;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>overlayWith<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>overlayImage <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// size is taken from the background image</span>
	UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span>self.size<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self drawAtPoint<span style="color: #002200;">:</span>CGPointZero<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>overlayImage drawAtPoint<span style="color: #002200;">:</span>CGPointZero<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/*
	// If Image Artifacts appear, replace the &quot;overlayImage drawAtPoint&quot; , method with the following
	// Yes, it's a workaround, yes I filed a bug report
	CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
	[overlayImage drawInRect:imageRect blendMode:kCGBlendModeOverlay alpha:0.999999999];
	*/</span>
&nbsp;
	UIImage <span style="color: #002200;">*</span>combinedImage <span style="color: #002200;">=</span> UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
	UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> combinedImage;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>An update to 334 <a href="http://www.trembl.org/codec/334/">Combining Images with UIImage &#038; CGContext – (Offscreen drawing)</a></p>
<p>(Did I say, how much I love Categories&#8230;?)</p>
<p><strong>Update</strong><br />
I came across some strange behaviour when layering a PNG image with transparency over another image. Did not show up in the Simulator, only in iPhone 3GS (and probably also on other devices.)<br />
The base image draws fine, but the overlay image appears to be truncated and the last pixels shifted, producing some bright green artifacts.<br />
Changing</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>overlayImage drawAtPoint<span style="color: #002200;">:</span>CGPointZero<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>to</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGRect imageRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, self.size.width, self.size.height<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span>overlayImage drawInRect<span style="color: #002200;">:</span>imageRect blendMode<span style="color: #002200;">:</span>kCGBlendModeOverlay alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>did not really help; the green artifacts remainded. It was strange though, that they did not appear in the other blendmodes. Using</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGContextDrawImage<span style="color: #002200;">&#40;</span>c, imageRect, <span style="color: #002200;">&#91;</span>overlayImage CGImage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>would also work, but then the images turn up upside down. Not what I really needed. (Yes, I know, there might not be a hard fix for that, but really &#8211; it should be that complicated.)</p>
<p>After playing a bit more with the values, I found, that setting alpha lower than 1.0 gets rid of the display artifact:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>overlayImage drawInRect<span style="color: #002200;">:</span>imageRect blendMode<span style="color: #002200;">:</span>kCGBlendModeOverlay alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">0.9999999</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Bug filed at Apple&#8217;s Bug Report, let&#8217;s see. Or maybe am I missing something here?</p>
<p>Anyway here they the files are, zipped and ready for <a href="http://www.trembl.org/codec/wp-content/uploads/2010/04/UIImage+Category.zip">download</a>.</p>
<p><strong>Post Scriptum</strong></p>
<p><a href="http://www.trembl.org/codec/wp-content/uploads/2010/04/Artifact.zip">Test Project</a>, showing the visual artifact in action. Only appears on the device, NOT IN THE SIMULATOR.</p>
<p><img src="http://www.trembl.org/codec/wp-content/uploads/2010/04/UIButtons+iPhone.png" width="414" height="770"  /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/569/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#import vs #include in Objective C &#8211; A quick reminder</title>
		<link>http://www.trembl.org/codec/560/</link>
		<comments>http://www.trembl.org/codec/560/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 06:56:31 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[include]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/560/</guid>
		<description><![CDATA[#import is identical to #include, except that it makes sure that the same file is never included more than once. It’s therefore preferred and is used in place of #include in code examples throughout Objective-C–based documentation. http://developer.apple.com/ mac/library/documentation/ cocoa/conceptual/ObjectiveC /Articles/ ocDefiningClasses.html]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>#import</strong> is identical to <strong>#include</strong>, except that it makes sure that the same file is never included more than once. It’s therefore preferred and is used in place of #include in code examples throughout Objective-C–based documentation.</p></blockquote>
<p><a href="http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjectiveC/Articles/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF123">http://developer.apple.com/ mac/library/documentation/ cocoa/conceptual/ObjectiveC /Articles/ ocDefiningClasses.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/560/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singleton Classes and Shared Resources in Objective-C</title>
		<link>http://www.trembl.org/codec/457/</link>
		<comments>http://www.trembl.org/codec/457/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 10:13:26 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[shared]]></category>
		<category><![CDATA[Singleton]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=457</guid>
		<description><![CDATA[Resource.h #import &#60;Cocoa/Cocoa.h&#62; &#160; @interface Resource : NSObject &#123; float myValue; &#125; &#160; @property float scale; &#160; + &#40;id&#41;shared; &#160; @end &#160; Resource.m #import &#34;Resource.h&#34; &#160; @implementation Resource &#160; @synthesize scale; &#160; - &#40;id&#41;init &#123; scale = 1.0f // my default value &#125; &#160; + &#40;id&#41;shared &#123; static Resource *sharedResource = nil; if &#40;!sharedResource&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p><em>Resource.h</em></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Cocoa/Cocoa.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Resource <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> myValue;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #a61390;">float</span> scale;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>shared;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>&nbsp;</p>
<p><em>Resource.m</em></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Resource.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Resource
&nbsp;
<span style="color: #a61390;">@synthesize</span> scale;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init <span style="color: #002200;">&#123;</span>
	scale <span style="color: #002200;">=</span> 1.0f	<span style="color: #11740a; font-style: italic;">// my default value</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>shared <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> Resource <span style="color: #002200;">*</span>sharedResource <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>sharedResource<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		sharedResource <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> sharedResource;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>An now it can be used like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">Resource <span style="color: #002200;">*</span>r <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Resource shared<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Resource Test: %f&quot;</span>, <span style="color: #002200;">&#91;</span>r myValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// Resource Test: 1.000000</span>
&nbsp;
<span style="color: #002200;">&#91;</span>r setMyValue<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Resource Test: %f&quot;</span>, <span style="color: #002200;">&#91;</span>r myValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//# Resource Test: 2.000000</span>
&nbsp;
Resource <span style="color: #002200;">*</span>s <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>Resource shared<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>s setMyValue<span style="color: #002200;">:</span><span style="color: #2400d9;">2.111</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Resource Test: %f&quot;</span>, <span style="color: #002200;">&#91;</span>s myValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// Resource Test: 2.111000</span>
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Resource Test: %f&quot;</span>, <span style="color: #002200;">&#91;</span>r myValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">//Resource Test: 2.111000</span></pre></div></div>

<p>Note that the values stays constant, even if another instantionation occurs. I guess that&#8217;s what Singleton classes are all about.</p>
<p>Adapted from Scott Stevenson&#8217;s <a href="http://cocoadevcentral.com/articles/000083.php#8">fantastic introduction</a> at <a href="http://cocoadevcentral.com/">Cocoa Dev Central</a>.</p>
<p>And more tutorials from Scott Stevenson&#8230;<br />
<a href="http://cocoadevcentral.com/articles/000081.php">Cocoa Dev Central: Learn C for Cocoa</a><br />
<a href="http://cocoadevcentral.com/d/learn_objectivec/">Cocoa Dev Central: Learn Objective C</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/457/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting current Time with Microseconds</title>
		<link>http://www.trembl.org/codec/420/</link>
		<comments>http://www.trembl.org/codec/420/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 08:22:09 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[micro]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[seconds]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[usec]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=420</guid>
		<description><![CDATA[#import &#38;lt;sys/time.h&#38;gt; &#160; struct&#60;/span&#62; timeval tv; gettimeofday&#40;&#38;tv, NULL&#41;; int sec = tv.tv_sec; int usec = tv.tv_usec; The system&#8217;s notion of the current Greenwich time and the current time zone is obtained with the gettimeofday() call, and set with the ettimeofday() call. The time is expressed in seconds and microseconds since midnight (0 hour), January 1, [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;sys/time.h&amp;gt;</span>
&nbsp;
struct&lt;<span style="color: #002200;">/</span>span&gt; timeval tv;
gettimeofday<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>tv, <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">int</span> sec <span style="color: #002200;">=</span> tv.tv_sec;
<span style="color: #a61390;">int</span> usec <span style="color: #002200;">=</span> tv.tv_usec;</pre></div></div>

<blockquote><p>The system&#8217;s notion of the current Greenwich time and the current time zone is obtained with the gettimeofday() call, and set with the ettimeofday() call.  The time is expressed in seconds and microseconds since midnight (0 hour), January 1, 1970.  The resolution of the system clock is hardware dependent, and the time may be updated continuously or in &#8220;ticks.&#8221;  If tp is NULL and tzp is non-NULL, gettimeofday() will opulate the timezone struct in tzp.  If tp is non-NULL and tzp is NULL, then only the timeval struct in tp is populated. If both tp and tzp are NULL, nothing is returned.</p></blockquote>
<p>Check out the man page for more info.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/420/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simulating Keyboard &amp; Mouse Events. And Key-Modifier Events</title>
		<link>http://www.trembl.org/codec/289/</link>
		<comments>http://www.trembl.org/codec/289/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 09:21:56 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[10.5]]></category>
		<category><![CDATA[10.6]]></category>
		<category><![CDATA[alt]]></category>
		<category><![CDATA[Cursor]]></category>
		<category><![CDATA[key]]></category>
		<category><![CDATA[Keyboard]]></category>
		<category><![CDATA[Mouse]]></category>
		<category><![CDATA[NSEvent]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Position]]></category>
		<category><![CDATA[shift]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=289</guid>
		<description><![CDATA[Creating and Posting a Keyboard Event: CGEventRef sDown, sUp; sDown = CGEventCreateKeyboardEvent &#40; NULL, &#40;CGKeyCode&#41;1, &#60;strong&#62;true&#60;/strong&#62; &#41;; &#60;strong class=&#34;red&#34;&#62;CGEventSetFlags&#40;sDown, kCGEventFlagMaskShift&#41;;&#60;/strong&#62; &#160; // setting flags with special function. &#60;em&#62;// Setting it via CGCreateKeyboardEvent // would work only for the first time it's run&#60;/em&#62; &#160; CGEventPost&#40;kCGHIDEventTap, sDown&#41;; &#160; sUp = CGEventCreateKeyboardEvent &#40; NULL, &#40;CGKeyCode&#41;1, &#60;strong&#62;false&#60;/strong&#62; &#41;; CGEventPost&#40;kCGHIDEventTap, [...]]]></description>
			<content:encoded><![CDATA[<p>Creating and Posting a Keyboard Event:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGEventRef sDown, sUp;
sDown <span style="color: #002200;">=</span> CGEventCreateKeyboardEvent <span style="color: #002200;">&#40;</span>
			<span style="color: #a61390;">NULL</span>,
			<span style="color: #002200;">&#40;</span>CGKeyCode<span style="color: #002200;">&#41;</span><span style="color: #2400d9;">1</span>,
			&lt;strong&gt;true&lt;<span style="color: #002200;">/</span>strong&gt;
<span style="color: #002200;">&#41;</span>;
&lt;strong class<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;red&quot;</span>&gt;CGEventSetFlags<span style="color: #002200;">&#40;</span>sDown, kCGEventFlagMaskShift<span style="color: #002200;">&#41;</span>;&lt;<span style="color: #002200;">/</span>strong&gt;  
&nbsp;
<span style="color: #11740a; font-style: italic;">// setting flags with special function. </span>
&lt;em&gt;<span style="color: #11740a; font-style: italic;">// Setting it via CGCreateKeyboardEvent</span>
<span style="color: #11740a; font-style: italic;">// would work only for the first time it's run&lt;/em&gt;</span>
&nbsp;
CGEventPost<span style="color: #002200;">&#40;</span>kCGHIDEventTap, sDown<span style="color: #002200;">&#41;</span>;
&nbsp;
sUp <span style="color: #002200;">=</span> CGEventCreateKeyboardEvent <span style="color: #002200;">&#40;</span>
			<span style="color: #a61390;">NULL</span>,
			<span style="color: #002200;">&#40;</span>CGKeyCode<span style="color: #002200;">&#41;</span><span style="color: #2400d9;">1</span>,
			&lt;strong&gt;false&lt;<span style="color: #002200;">/</span>strong&gt;
<span style="color: #002200;">&#41;</span>;
CGEventPost<span style="color: #002200;">&#40;</span>kCGHIDEventTap, sUp<span style="color: #002200;">&#41;</span>;
&nbsp;
CFRelease<span style="color: #002200;">&#40;</span>sDown<span style="color: #002200;">&#41;</span>;
CFRelease<span style="color: #002200;">&#40;</span>sUp<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>That leaves the door open for applying the same to mouse events:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGEventRef mouseEvent;
mouseEvent <span style="color: #002200;">=</span> CGEventCreateMouseEvent <span style="color: #002200;">&#40;</span>
			<span style="color: #a61390;">NULL</span>,
			kCGEventMouseMoved,
			CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>,
			kCGMouseButtonLeft
<span style="color: #002200;">&#41;</span>;
CGEventPost<span style="color: #002200;">&#40;</span>kCGHIDEventTap, mouseEvent <span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Magical. Isn&#8217;t it.</p>
<p><em>Of course, in pre-10.6 days it would have looked like that:</em></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGPostKeyboardEvent <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">5</span>,<span style="color: #a61390;">true</span><span style="color: #002200;">&#41;</span>;
CGPostKeyboardEvent <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">5</span>,<span style="color: #a61390;">false</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/289/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UIWebView &#8211; checking when user clicks a link</title>
		<link>http://www.trembl.org/codec/275/</link>
		<comments>http://www.trembl.org/codec/275/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 10:03:29 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[UIWebView]]></category>
		<category><![CDATA[UIWebViewNavigationType]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=275</guid>
		<description><![CDATA[- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // intercepting web click in the webView NSLog(@"%@, %i", [request.URL absoluteString], navigationType); } Don&#8217;t forget to set &#60;UIWebViewDelegate&#62;]]></description>
			<content:encoded><![CDATA[<p><code>- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {</p>
<p>	// intercepting web click in the webView<br />
	NSLog(@"%@,  %i", [request.URL absoluteString], navigationType);<br />
}<br />
</code></p>
<p>Don&#8217;t forget to set &lt;UIWebViewDelegate&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/275/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caching on Objective-C with NSURLCache</title>
		<link>http://www.trembl.org/codec/269/</link>
		<comments>http://www.trembl.org/codec/269/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 07:43:05 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[cachePolicy cache]]></category>
		<category><![CDATA[NSURLCache]]></category>
		<category><![CDATA[NSURLRequest]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[request]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=269</guid>
		<description><![CDATA[A quick reminder on how caching works in Objective-C. Initially there is one cache, NSURLCache administrates it. Get the object: NSURLCache *cache = &#91;NSURLCache sharedURLCache&#93; &#160; &#91;cache memoryCapacity&#93;; &#91;cache setMemoryCapacity: 1024*1024*1&#93;; // in byte &#91;cache currentMemoryUsage&#93;; Don&#8217;t worry about the disk aka the flash memory in iPhone, it&#8217;s not accessible. A bit of strange behaviour, [...]]]></description>
			<content:encoded><![CDATA[<p>A quick reminder on how caching works in Objective-C.</p>
<p>Initially there is one cache, NSURLCache administrates it.</p>
<p>Get the object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURLCache</span> <span style="color: #002200;">*</span>cache <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLCache</span> sharedURLCache<span style="color: #002200;">&#93;</span>
&nbsp;
<span style="color: #002200;">&#91;</span>cache memoryCapacity<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>cache setMemoryCapacity<span style="color: #002200;">:</span> <span style="color: #2400d9;">1024</span><span style="color: #002200;">*</span><span style="color: #2400d9;">1024</span><span style="color: #002200;">*</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// in byte</span>
<span style="color: #002200;">&#91;</span>cache currentMemoryUsage<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Don&#8217;t worry about the disk <em>aka the flash memory</em> in iPhone, it&#8217;s not accessible.</p>
<p>A bit of strange behaviour, either a bug in the software or in my brain:<br />
When changing ViewControllers the memoryCapacity seems to reset itself to 0, although previous caches remain intact. Re-setting the memoryCapacity back to the original level seems to solve this. (The cache is not overwritten.)</p>
<p>Using the Cache<br />
The whole point of using the cache is to not download online material over again. The following steps make for a happy cache:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//URLRequest</span>
<span style="color: #400080;">NSURLRequest</span> <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLRequest</span> requestWithURL<span style="color: #002200;">:</span>theURL
cachePolicy<span style="color: #002200;">:</span>NSURLRequestUseProtocolCachePolicy timeoutInterval<span style="color: #002200;">:</span>10.0f<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The delegate response would allow you to alter the about-to-be-cached-data, leaving it in is not necessary:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>c
willCacheResponse<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSCachedURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response <span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;willCacheResponse&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">return</span> response;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>All in all, not that much to take it. Finding out, that the memoryCapacity gets reset was the most time-consuming bit.<br />
For another example, of how to roll your own cache, look at <a href="http://developer.apple.com/iphone/library/samplecode/URLCache/index.html">URLCache</a> example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/269/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Like explode(), only componentsSeparatedByString:</title>
		<link>http://www.trembl.org/codec/265/</link>
		<comments>http://www.trembl.org/codec/265/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 10:53:17 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[componentsSeparatedByString]]></category>
		<category><![CDATA[explode]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=265</guid>
		<description><![CDATA[PHP explode(&#8220;, &#8221; , &#8220;One, Two, Three&#8221;); Objective-C NSArray *listItems = [@"One, Two, Three" componentsSeparatedByString:@", "];]]></description>
			<content:encoded><![CDATA[<p><strong>PHP</strong><br />
explode(&#8220;, &#8221; , &#8220;One, Two, Three&#8221;);</p>
<p><strong>Objective-C</strong><br />
NSArray *listItems = [@"One, Two, Three" componentsSeparatedByString:@", "];</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/265/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The mystery of self.* &#8211; resolved?</title>
		<link>http://www.trembl.org/codec/259/</link>
		<comments>http://www.trembl.org/codec/259/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 11:50:26 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[self]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=259</guid>
		<description><![CDATA[self If you want to access a property of self using accessor methods, you must explicitly call out self as illustrated in this example: self.age = 10; If you do not use self., you access the instance variable directly. In the following example, the set accessor method for the age property is not invoked: age [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>self</strong><br />
If you want to access a property of self using accessor methods, you must explicitly call out self as illustrated in this example:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">self.age <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span>;</pre></div></div>

<p>If you do not use self., you access the instance variable directly. In the following example, the set<br />
accessor method for the age property is not invoked:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">age <span style="color: #002200;">=</span> <span style="color: #2400d9;">10</span>;</pre></div></div>

</blockquote>
<p>p57, The Objective-C 2.0 Programming Language Language (v2008-06-09)<br />
<a href="http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW24">p22, The Objective-C 2.0 Programming Language Language (v2009-10-19)</a></p>
<p>So basically it&#8217;s a shortcut to:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span><span style="color: #002200;">*</span> a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self age<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>self setAge<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>(providing there is a setter/getter for <em>age</em>.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/259/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stepping over an Array (or Dictionary)</title>
		<link>http://www.trembl.org/codec/243/</link>
		<comments>http://www.trembl.org/codec/243/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 09:20:57 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[2.0]]></category>
		<category><![CDATA[Enumberation]]></category>
		<category><![CDATA[Fast]]></category>
		<category><![CDATA[NSArray]]></category>
		<category><![CDATA[NSDIctionary]]></category>
		<category><![CDATA[NSEnumerator]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=243</guid>
		<description><![CDATA[NSArray *paths = &#91;&#91;NSFileManager defaultManager&#93; directoryContentsAtPath: NSHomeDirectory&#40;&#41;&#93;; &#60;del datetime=&#34;2010-04-01T02:49:15+00:00&#34;&#62; // either like that... NSEnumerator *e = &#91;paths objectEnumerator&#93;; for &#40;NSString *p in e&#41; &#123; NSLog&#40;@&#34;path: %@&#34;, p&#41;; &#125; &#160; // or like that. for &#40;NSString *p in &#91;paths objectEnumerator&#93;&#41; &#123; NSLog&#40;@&#34;path: %@&#34;, p&#41;; &#125; NSDictionary has both [myDict objectEnumerator] and [myDict keyEnumerator]. Fast Enumberation is [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span> 
	directoryContentsAtPath<span style="color: #002200;">:</span> NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&lt;del datetime<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;2010-04-01T02:49:15+00:00&quot;</span>&gt;
<span style="color: #11740a; font-style: italic;">// either like that...</span>
<span style="color: #400080;">NSEnumerator</span> <span style="color: #002200;">*</span>e <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>paths objectEnumerator<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>p <span style="color: #a61390;">in</span> e<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;path: %@&quot;</span>, p<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// or like that.</span>
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>p <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>paths objectEnumerator<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;path: %@&quot;</span>, p<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>NSDictionary has both <code>[myDict objectEnumerator]</code> and <code>[myDict keyEnumerator]</code>.</del></p>
<p>Fast Enumberation is a feature in Objective-C 2.0, it allows you to step over arrays and dictionaries in a fast, consice, and secure (guarded against mutations) manner.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>p <span style="color: #a61390;">in</span> paths<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;path: %@&quot;</span>, p<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// or</span>
&nbsp;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>p;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>p <span style="color: #a61390;">in</span> paths<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;path: %@&quot;</span>, p<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Sometimes things are really as simple as they should be.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/243/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
