<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Cocoa for Breakfast</title>
	<atom:link href="http://cocoaforbreakfast.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://cocoaforbreakfast.wordpress.com</link>
	<description>Cool recipes for your iOS development</description>
	<lastBuildDate>Mon, 30 Jan 2012 12:15:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='cocoaforbreakfast.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/3b58c5578f9303b09a84ed70c5e7e004?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Cocoa for Breakfast</title>
		<link>http://cocoaforbreakfast.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://cocoaforbreakfast.wordpress.com/osd.xml" title="Cocoa for Breakfast" />
	<atom:link rel='hub' href='http://cocoaforbreakfast.wordpress.com/?pushpress=hub'/>
		<item>
		<title>HTPP you said? Fix URLs easily</title>
		<link>http://cocoaforbreakfast.wordpress.com/2011/07/12/htpp-you-said-fix-urls-easily/</link>
		<comments>http://cocoaforbreakfast.wordpress.com/2011/07/12/htpp-you-said-fix-urls-easily/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 19:54:20 +0000</pubDate>
		<dc:creator>Jorge Bernal</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Foundation]]></category>
		<category><![CDATA[NSRegularExpression]]></category>
		<category><![CDATA[NSURL]]></category>

		<guid isPermaLink="false">http://cocoaforbreakfast.wordpress.com/?p=62</guid>
		<description><![CDATA[If you have any text field in your app where the users have to input a URL, there&#8217;s going to be trouble. Should they type the http prefix? What if they miss the colon, or mistype http? The first case is kind of obvious, and we&#8217;ve had this on the WordPress app for a while: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cocoaforbreakfast.wordpress.com&amp;blog=20475409&amp;post=62&amp;subd=cocoaforbreakfast&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you have any text field in your app where the users have to input a URL, there&#8217;s going to be trouble. Should they type the http prefix? What if they miss the colon, or mistype <em>http</em>?</p>
<p>The first case is kind of obvious, and we&#8217;ve had this on the WordPress app for a while:</p>
<p><pre class="brush: objc;">
NSString *urlToValidate = urlTextField.text;	
if(![urlToValidate hasPrefix:@&quot;http&quot;])
    urlToValidate = [NSString stringWithFormat:@&quot;http://%@&quot;, url];
</pre></p>
<p>That was OK, but clearly not enough. Some users complained that they couldn&#8217;t add their blogs to the app, and looking at the <a href="http://cocoaforbreakfast.wordpress.com/2011/02/25/logging-into-files-for-ios/" title="Logging into files for&nbsp;iOS">log files</a>, the reason was obvious. They were typing things like &#8220;http:myblog.com&#8221;, &#8220;http//myblog.com&#8221;, &#8220;hppt://myblog.com&#8221;, or any other combination you can think of.</p>
<p>So we need to get a bit more sophisticated. Since iOS 4.0 you can use regular expressions natively with <a href="http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html">NSRegularExpression</a> (or go for <a href="http://regexkit.sourceforge.net/RegexKitLite/">RegexKitLite</a> if you need 3.0 compatibility), so we are going to use two of them.</p>
<blockquote><p>
^https?://
</p></blockquote>
<p>First, we check if the typed url starts with <em>http://</em> or <em>https://</em>. If that&#8217;s the case, everything looks good</p>
<blockquote><p>
^h[htp]{1,3}s?[:/]/*
</p></blockquote>
<p>The second one is a bit trickier, but not the <a href="http://stackoverflow.com/questions/800813/what-is-the-most-difficult-challenging-regular-expression-you-have-ever-written">most complicated regular expression</a> you could find. It means:</p>
<ul>
<li>Starts with &#8220;h&#8221;</li>
<li>Then between 1 and 3 of &#8220;h&#8221;, &#8220;t&#8221;, or &#8220;p&#8221;. This covers htpp, hppt, htt,&#8230;</li>
<li>An optional &#8220;s&#8221;. It could be a mistyped https url</li>
<li>Either a &#8220;:&#8221; or a &#8220;/&#8221;</li>
<li>Zero or more &#8220;/&#8221;</li>
</ul>
<p>Note that this would also match a valid &#8220;http://&#8221; prefix, but that&#8217;s why we are using the other first. After a bit of testing, I made a NSURL category with the <em>guessURLWithString</em> method. If you find any errors or any URL that isn&#8217;t properly fixed, please leave a comment.</p>
<p><pre class="brush: objc;">
@implementation NSURL (NSURL_Guess)
+ (id)guessURLWithString:(NSString *)URLString {
    NSError *error = nil;
    NSRegularExpression *okExpression = [NSRegularExpression regularExpressionWithPattern:@&quot;^https?://&quot; options:NSRegularExpressionCaseInsensitive error:&amp;amp;error];
    if (error) {
        NSLog(@&quot;Invalid regular expression: %@&quot;, [error localizedDescription]);
        return nil;
    }

    NSRegularExpression *wrongExpression = [NSRegularExpression regularExpressionWithPattern:@&quot;^(h[htp]{1,3}s?[:/]/*)&quot; options:NSRegularExpressionCaseInsensitive error:&amp;amp;error];
    if (error) {
        NSLog(@&quot;Invalid regular expression: %@&quot;, [error localizedDescription]);
        return nil;
    }

    NSString *fixedURLString;
    if ([okExpression numberOfMatchesInString:URLString options:0 range:NSMakeRange(0, [URLString length])]) {
        fixedURLString = URLString;
    } else if ([wrongExpression numberOfMatchesInString:URLString options:0 range:NSMakeRange(0, [URLString length])]) {
        fixedURLString = [wrongExpression stringByReplacingMatchesInString:URLString options:0 range:NSMakeRange(0, [URLString length]) withTemplate:@&quot;http://&quot;];
    } else {
        fixedURLString = [NSString stringWithFormat:@&quot;http://%@&quot;, URLString];
    }

    return [NSURL URLWithString:fixedURLString];
}
@end
</pre></p>
<p>You can download the code from <a href="https://github.com/koke/NSURL-Guess">GitHub</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cocoaforbreakfast.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cocoaforbreakfast.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cocoaforbreakfast.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cocoaforbreakfast.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cocoaforbreakfast.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cocoaforbreakfast.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cocoaforbreakfast.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cocoaforbreakfast.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cocoaforbreakfast.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cocoaforbreakfast.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cocoaforbreakfast.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cocoaforbreakfast.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cocoaforbreakfast.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cocoaforbreakfast.wordpress.com/62/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cocoaforbreakfast.wordpress.com&amp;blog=20475409&amp;post=62&amp;subd=cocoaforbreakfast&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cocoaforbreakfast.wordpress.com/2011/07/12/htpp-you-said-fix-urls-easily/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e9d3daa35eb952be205963e6909a2a95?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokejb</media:title>
		</media:content>
	</item>
		<item>
		<title>Time profiler: when a small change can bring huge gains</title>
		<link>http://cocoaforbreakfast.wordpress.com/2011/03/01/time-profiler-when-a-small-change-can-bring-huge-gains/</link>
		<comments>http://cocoaforbreakfast.wordpress.com/2011/03/01/time-profiler-when-a-small-change-can-bring-huge-gains/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 13:02:13 +0000</pubDate>
		<dc:creator>Jorge Bernal</dc:creator>
				<category><![CDATA[Performance]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Instruments]]></category>
		<category><![CDATA[Profiling]]></category>
		<category><![CDATA[Time Profiler]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://cocoaforbreakfast.wordpress.com/?p=51</guid>
		<description><![CDATA[Sometimes we get complaints about how slow is our app. Believe me, I&#8217;ve seen worse, and we are focused on stability right now. However, I was curious to get a hint of how much could we improved. So I launched Instruments from Run &#62; Run with Performance Tool &#62; Time Profiler, and went for a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cocoaforbreakfast.wordpress.com&amp;blog=20475409&amp;post=51&amp;subd=cocoaforbreakfast&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://cocoaforbreakfast.files.wordpress.com/2011/03/instruments.jpg"><img class="aligncenter size-full wp-image-52" title="Time Profiler" src="http://cocoaforbreakfast.files.wordpress.com/2011/03/instruments.jpg?w=594&#038;h=426" alt="" width="594" height="426" /></a>Sometimes we get complaints about how slow is <a href="http://ios.wordpress.org/">our app</a>. Believe me, I&#8217;ve seen worse, and we are focused on stability right now. However, I was curious to get a hint of how much could we improved.</p>
<p>So I launched Instruments from <em>Run &gt; Run with Performance Tool &gt; Time Profiler</em>, and went for a first run: about 6.5 seconds.</p>
<p>As you can see in the image, it gets better. You can play with the settings in the bottom left pane, but for a launch <em>Separate by thread</em> and <em>Hide System Libraries</em> are a good pick.</p>
<p>There are two places that look good to optimize: checkIfStatsShouldRun and persistentStoreCoordinator.</p>
<ul>
<li>The first (checkIfStatsShouldRun) is the first thing called in applicationDidFinishLaunching, but it needs to access the core data objects, and also sends a connection request synchronously. I moved later into the initialization process, and made the <em>runStats</em> method run in the background.</li>
<li>As for the second, it was a remainder of the migration development. We didn&#8217;t delete the old data file, so every launch the app tried to import the old blogs, wasting a precious amount of time. Now, after a successful import the file is deleted, and no more time is lost.</li>
</ul>
<p>A <a href="http://ios.trac.wordpress.org/changeset/1740">few changes</a>, ten minutes of looking around, and now it launches in less than 3 seconds. I also found some other things worth improving, but those require deeper changes, and now it&#8217;s not the time for that.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cocoaforbreakfast.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cocoaforbreakfast.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cocoaforbreakfast.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cocoaforbreakfast.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cocoaforbreakfast.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cocoaforbreakfast.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cocoaforbreakfast.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cocoaforbreakfast.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cocoaforbreakfast.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cocoaforbreakfast.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cocoaforbreakfast.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cocoaforbreakfast.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cocoaforbreakfast.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cocoaforbreakfast.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cocoaforbreakfast.wordpress.com&amp;blog=20475409&amp;post=51&amp;subd=cocoaforbreakfast&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cocoaforbreakfast.wordpress.com/2011/03/01/time-profiler-when-a-small-change-can-bring-huge-gains/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://cocoaforbreakfast.files.wordpress.com/2011/03/instruments.jpg?w=150" />
		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/03/instruments.jpg?w=150" medium="image">
			<media:title type="html">Time Profiler</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/e9d3daa35eb952be205963e6909a2a95?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokejb</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/03/instruments.jpg" medium="image">
			<media:title type="html">Time Profiler</media:title>
		</media:content>
	</item>
		<item>
		<title>Logging into files for iOS</title>
		<link>http://cocoaforbreakfast.wordpress.com/2011/02/25/logging-into-files-for-ios/</link>
		<comments>http://cocoaforbreakfast.wordpress.com/2011/02/25/logging-into-files-for-ios/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 01:46:55 +0000</pubDate>
		<dc:creator>Jorge Bernal</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Log]]></category>
		<category><![CDATA[NSLog]]></category>

		<guid isPermaLink="false">http://cocoaforbreakfast.wordpress.com/?p=23</guid>
		<description><![CDATA[Do you sometimes wish users could send you the output of the console with the crash log? NSLog is really helpful for debugging, but there is no way (or I haven&#8217;t found it) to get the console output unless you are running a debug build from Xcode. This small class can log to a file [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cocoaforbreakfast.wordpress.com&amp;blog=20475409&amp;post=23&amp;subd=cocoaforbreakfast&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/wordpress-debugger-console.jpg"><img class="aligncenter size-full wp-image-29" title="WordPress - Debugger Console" src="http://cocoaforbreakfast.files.wordpress.com/2011/02/wordpress-debugger-console-e1298598225854.jpg?w=594&#038;h=111" alt="" width="594" height="111" /></a>Do you sometimes wish users could send you the output of the console with the crash log?</p>
<p>NSLog is really helpful for debugging, but there is no way (or I haven&#8217;t found it) to get the console output unless you are running a debug build from Xcode. This small class can log to a file while also printing to the console. Just import it and start using FLog instead of NSLog.</p>
<p>FileLogger.h:</p>
<p><pre class="brush: objc;">
@interface FileLogger : NSObject {
 NSFileHandle *logFile;
}
+ (FileLogger *)sharedInstance;
- (void)log:(NSString *)format, ...;
@end

#define FLog(fmt, ...) [[FileLogger sharedInstance] log:fmt, ##__VA_ARGS__]
</pre></p>
<p>FileLogger.m:</p>
<p><pre class="brush: objc;">
#import &quot;FileLogger.h&quot;

@implementation FileLogger
- (void)dealloc {
 [logFile release]; logFile = nil;
 [super dealloc];
}

- (id) init {
  if (self == [super init]) {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@&quot;application.log&quot;];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath])
      [fileManager createFileAtPath:filePath
                           contents:nil
                         attributes:nil];
    logFile = [[NSFileHandle fileHandleForWritingAtPath:filePath] retain];
    [logFile seekToEndOfFile];
  }

  return self;
}

- (void)log:(NSString *)format, ... {
  va_list ap;
  va_start(ap, format);

  NSString *message = [[NSString alloc] initWithFormat:format arguments:ap];
  NSLog(message);
  [logFile writeData:[[message stringByAppendingString:@&quot;\n&quot;] dataUsingEncoding:NSUTF8StringEncoding]];
  [logFile synchronizeFile];

  [message release];
}

+ (FileLogger *)sharedInstance {
  static FileLogger *instance = nil;
  if (instance == nil) instance = [[FileLogger alloc] init];
  return instance;
}
@end

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cocoaforbreakfast.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cocoaforbreakfast.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cocoaforbreakfast.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cocoaforbreakfast.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cocoaforbreakfast.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cocoaforbreakfast.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cocoaforbreakfast.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cocoaforbreakfast.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cocoaforbreakfast.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cocoaforbreakfast.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cocoaforbreakfast.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cocoaforbreakfast.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cocoaforbreakfast.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cocoaforbreakfast.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cocoaforbreakfast.wordpress.com&amp;blog=20475409&amp;post=23&amp;subd=cocoaforbreakfast&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cocoaforbreakfast.wordpress.com/2011/02/25/logging-into-files-for-ios/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:thumbnail url="http://cocoaforbreakfast.files.wordpress.com/2011/02/wordpress-debugger-console-e1298598225854.jpg?w=150" />
		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/wordpress-debugger-console-e1298598225854.jpg?w=150" medium="image">
			<media:title type="html">WordPress - Debugger Console</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/e9d3daa35eb952be205963e6909a2a95?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokejb</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/wordpress-debugger-console-e1298598225854.jpg" medium="image">
			<media:title type="html">WordPress - Debugger Console</media:title>
		</media:content>
	</item>
		<item>
		<title>EXC_BAD_ACCESS, a Zombie nightmare</title>
		<link>http://cocoaforbreakfast.wordpress.com/2011/02/25/exc_bad_access-a-zombie-nightmare/</link>
		<comments>http://cocoaforbreakfast.wordpress.com/2011/02/25/exc_bad_access-a-zombie-nightmare/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 00:51:15 +0000</pubDate>
		<dc:creator>Jorge Bernal</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Instruments]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSZombie]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://cocoaforbreakfast.wordpress.com/?p=4</guid>
		<description><![CDATA[Debugging Objective-C on the iPhone is usually OK, but there are some painful moments with those hard to find bugs. <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cocoaforbreakfast.wordpress.com&amp;blog=20475409&amp;post=4&amp;subd=cocoaforbreakfast&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Debugging Objective-C on the iPhone is usually OK, but there are some painful moments with those hard to find bugs. Today&#8217;s bug went something like this:</p>
<blockquote><p>1.) Open iPad app in portrait mode<br />
2.) Click on &#8220;My Blogs&#8221; top left and choose a blog<br />
3.) Make sure you are on the comments view and click &#8216;edit&#8217; at the top right of that dialog (the button changes to &#8216;cancel&#8217;)<br />
4.) Then when the list is in edit mode rotate the device to landscape mode<br />
5.) The &#8216;cancel&#8217; button now says &#8216;edit&#8217;. click on this &#8216;edit&#8217; button.<br />
6.) It should work fine, now simply put the device in portrait mode again and continue from step 2 above.<br />
It should crash the second time you do this, if not it will crash on the third time.</p></blockquote>
<p>Nice catch. When you are debugging with Xcode, most of the times you get some useful message in the console or at least some stack trace pointing where things went wrong. Today&#8217;s message was this useful</p>
<blockquote><p>Program received signal:  “EXC_BAD_ACCESS”.</p></blockquote>
<p>And the stack trace was no better:</p>
<h2><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/asm-objc_msgsend-0x01fa7a4c_-wordpress-debugger.jpg"><img class="aligncenter size-full wp-image-5" title="Empty stack trace" src="http://cocoaforbreakfast.files.wordpress.com/2011/02/asm-objc_msgsend-0x01fa7a4c_-wordpress-debugger.jpg?w=594&#038;h=509" alt="" width="594" height="509" /></a></h2>
<h2><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/asm-objc_msgsend-0x01fa7a4c_-wordpress-debugger.jpg"></a>NSZombie to the rescue</h2>
<p>If you have worked with Cocoa for a while, you might be familiar with NSZombie, but since the development tools have changed in the last years, some of the tutorials around are not accurate.</p>
<p>NSZombie is a Cocoa trick that turns any object going to be deallocated into a NSZombie instance. You are going to crash anyway, but instead of Xcode just complaining that you tried to access the wrong memory address, you get something a bit better like this:</p>
<blockquote><p>2011-02-25 01:27:57.063 WordPress[10496:207] *** -[CFString isEqualToString:]: message sent to deallocated instance 0xcf1b0c0</p></blockquote>
<p>Not much, but at least you know that the guilty object was a string.</p>
<p>But there&#8217;s more to it. First, you enable NSZombie by double clicking your executable in the project info, and then in the &#8220;Arguments&#8221; tab setting the NSZombieEnabled environment variable</p>
<p><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/gravatarimageview-m-wordpress.jpg"><img class="aligncenter size-full wp-image-6" title="Executables" src="http://cocoaforbreakfast.files.wordpress.com/2011/02/gravatarimageview-m-wordpress.jpg?w=594" alt=""   /></a></p>
<p><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/gravatarimageview-m-wordpress.jpg"></a><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/executable-e2809cwordpresse2809d-info.jpg"><img class="aligncenter size-full wp-image-7" title="Executable “WordPress” Info" src="http://cocoaforbreakfast.files.wordpress.com/2011/02/executable-e2809cwordpresse2809d-info.jpg?w=594&#038;h=469" alt="" width="594" height="469" /></a></p>
<h2>Instruments, that wonderful tool</h2>
<p>Sometimes, it will be enough just knowing which class was released more than it should. But when it&#8217;s not, Instruments can help.</p>
<p>Sadly, Instruments can only do NSZombie magic on the simulator, but for this example that was not an issue. From Xcode, go to the &#8220;Run&#8221; menu, then &#8220;Run with Performance Tool&#8221;, and there you&#8217;ll probably see Zombies disabled. Don&#8217;t worry, select &#8220;Allocations&#8221; and you will be fine.</p>
<p>As soon as the app starts running, stop it by clicking on the big record button, we need to set up a few things first.</p>
<p><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments.jpg"><img class="aligncenter size-full wp-image-8" title="Instruments" src="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments.jpg?w=594" alt=""   /></a>Click on the little &#8220;info&#8221; button besides Allocations, and enable &#8220;Record reference counts&#8221; and &#8220;Enable NSZombie detection&#8221;. Start the app again (with the same big record button) and repeat the steps that led to the crash. Same story, but this time&#8230;</p>
<p><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments-2.jpg"><img class="aligncenter size-full wp-image-9" title="Instruments NSZombie" src="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments-2.jpg?w=594&#038;h=418" alt="" width="594" height="418" /></a>And when you click on that memory address, it will show you the history of the object, when and where it was created, and every time it was retained or released until it crashed</p>
<p><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/dock-1.jpg"><img class="aligncenter size-full wp-image-10" title="NSZombie history" src="http://cocoaforbreakfast.files.wordpress.com/2011/02/dock-1.jpg?w=594&#038;h=244" alt="" width="594" height="244" /></a>And even more, if you double-click that line, it will take you to a code editor, at the exact line when it was released. Now, that was helpful</p>
<p><a href="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments-1.jpg"><img class="aligncenter size-full wp-image-11" title="Instruments editor" src="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments-1.jpg?w=594" alt=""   /></a>And as you can see from this piece of code, we were releasing <em>email</em> on dealloc, but we didn&#8217;t retain it on <em>setEmail</em>. Delete that <em>[email release]</em> and problem fixed.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cocoaforbreakfast.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cocoaforbreakfast.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/cocoaforbreakfast.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/cocoaforbreakfast.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/cocoaforbreakfast.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/cocoaforbreakfast.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/cocoaforbreakfast.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/cocoaforbreakfast.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/cocoaforbreakfast.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/cocoaforbreakfast.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/cocoaforbreakfast.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/cocoaforbreakfast.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/cocoaforbreakfast.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/cocoaforbreakfast.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cocoaforbreakfast.wordpress.com&amp;blog=20475409&amp;post=4&amp;subd=cocoaforbreakfast&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cocoaforbreakfast.wordpress.com/2011/02/25/exc_bad_access-a-zombie-nightmare/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:thumbnail url="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments.jpg?w=150" />
		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments.jpg?w=150" medium="image">
			<media:title type="html">Instruments</media:title>
		</media:content>

		<media:content url="http://0.gravatar.com/avatar/e9d3daa35eb952be205963e6909a2a95?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokejb</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/asm-objc_msgsend-0x01fa7a4c_-wordpress-debugger.jpg" medium="image">
			<media:title type="html">Empty stack trace</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/gravatarimageview-m-wordpress.jpg" medium="image">
			<media:title type="html">Executables</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/executable-e2809cwordpresse2809d-info.jpg" medium="image">
			<media:title type="html">Executable “WordPress” Info</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments.jpg" medium="image">
			<media:title type="html">Instruments</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments-2.jpg" medium="image">
			<media:title type="html">Instruments NSZombie</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/dock-1.jpg" medium="image">
			<media:title type="html">NSZombie history</media:title>
		</media:content>

		<media:content url="http://cocoaforbreakfast.files.wordpress.com/2011/02/instruments-1.jpg" medium="image">
			<media:title type="html">Instruments editor</media:title>
		</media:content>
	</item>
	</channel>
</rss>
