<?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>iOS Developer Tips Blog &#187; Debugging</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/debugging/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:40:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Debugging with GDB: Print-Object and UIView recursiveDescription</title>
		<link>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-print-object-and-uiview-recursivedescription.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-print-object-and-uiview-recursivedescription.html#comments</comments>
		<pubDate>Mon, 30 Jan 2012 07:09:56 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10996</guid>
		<description><![CDATA[In the post Debugging with GDB: Introduction to Commands, Print and Print-Object I covered the basics of the command line inteface in GDB. In this post I&#8217;ll show you a trick to print out the entire view hierarchy for UIView objects. In Objective-C, all objects (derived from NSObject) have a description method, which returns an [...]]]></description>
			<content:encoded><![CDATA[<p>In the post <a href="http://iphonedevelopertips.com/debugging/debugging-with-gdb-introduction-to-commands.html">Debugging with GDB: Introduction to Commands, Print and Print-Object</a> I covered the basics of the command line inteface in GDB. In this post I&#8217;ll show you a trick to print out the entire view hierarchy for <strong>UIView</strong> objects.</p>
<p>In Objective-C, all objects (derived from <strong>NSObject</strong>) have a description method, which returns an <strong>NSString</strong> describing the object. When using GDB, you can print the description using print-object, for example, let&#8217;s use the interface definition below to look at a few descriptions:<br />
<span id="more-10996"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> SandboxViewController <span style="color: #002200;">:</span> UIViewController &lt;UITextFieldDelegate&gt;
<span style="color: #002200;">&#123;</span>
  UITextField  <span style="color: #002200;">*</span>username;
  UIButton     <span style="color: #002200;">*</span>testButton;
  UILabel      <span style="color: #002200;">*</span>label;
  UIView       <span style="color: #002200;">*</span>view;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When stopped on a breakpoint in the <strong>SandboxViewController</strong> object, GDB can display information on each object:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/po1.gif" alt="" title="po1" width="588" height="120" class="alignnone size-full wp-image-11005" /></p>
<p>Note, <strong>po</strong> is equivalent to <strong>print-object</strong>.</p>
<h5>Print-Object and recursiveDescription</h5>
<p>When working with <strong>UIView</strong> objects, there is an additional method for obtaining information, <strong>recursiveDescription</strong>. When using print-object and calling this method you can see the entire view hierarchy for the object.</p>
<p>With the <strong>SandboxViewController</strong> object defined above, in the <strong>loadView</strong> method I created a <strong>UIView</strong> as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self setView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Within GDB, when on a breakpoint within the <strong>SandboxViewController</strong> object, I can now view the entire view hierarchy as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/po2.gif" alt="" title="po2" width="589" height="209" class="alignnone size-full wp-image-11008" /></p>
<p>The same idea works for a <strong>UIView</strong> that is not associated with a view controller. Using the view defined below, and the label added as a subview:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create UIView</span>
view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">300</span>, <span style="color: #2400d9;">50</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>view<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Add label to UIView</span>
UILabel <span style="color: #002200;">*</span>tmplabel <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">280</span>, <span style="color: #2400d9;">80</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;    
<span style="color: #002200;">&#91;</span>tmplabel setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>tmplabel setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentLeft<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>tmplabel setText<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Enter your password:&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>view addSubview<span style="color: #002200;">:</span>tmplabel<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Here is the recursive description:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/po3.gif" alt="" title="po3" width="590" height="55" class="alignnone size-full wp-image-11013" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-print-object-and-uiview-recursivedescription.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Updated: Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled in Xcode 4</title>
		<link>http://iPhoneDeveloperTips.com/debugging/updated-tracking-down-exc_bad_access-errors-with-nszombieenabled-in-xcode-4.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/updated-tracking-down-exc_bad_access-errors-with-nszombieenabled-in-xcode-4.html#comments</comments>
		<pubDate>Fri, 27 Jan 2012 03:04:16 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10972</guid>
		<description><![CDATA[In a previous post Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled I explained how the environment variable NSZombieEnabled can help track down EXC_BAD_ACCESS errors, which are typically caused by attempting to access objects that have already been released. With Xcode 4 the process for setting NSZombieEnabled is different than in earlier versions of Xcode. To configure [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post <a href="http://iphonedevelopertips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html">Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled</a> I explained how the environment variable NSZombieEnabled can help track down EXC_BAD_ACCESS errors, which are typically caused by attempting to access objects that have already been released.</p>
<p>With Xcode 4 the process for setting NSZombieEnabled is different than in earlier versions of Xcode. To configure zombies, enter ⌥-⌘-r (alt-command-r). Once the dialog is shown, choose the option <strong>Enable Zombie Objects</strong><br />
<span id="more-10972"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/Zombie.gif" alt="" title="Zombie" width="429" height="349" class="alignnone size-full wp-image-10993" /></p>
<p>When using a zombie, any message sent to an object that has already released will result in an exception being thrown, which will look similar to the screenshot below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/exc_error.gif" alt="" title="exc_error" width="551" height="45" class="alignnone size-full wp-image-10990" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/updated-tracking-down-exc_bad_access-errors-with-nszombieenabled-in-xcode-4.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Debugging with GDB: Introduction to Commands, Print and Print-Object</title>
		<link>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-introduction-to-commands.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-introduction-to-commands.html#comments</comments>
		<pubDate>Tue, 10 Jan 2012 08:01:40 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10886</guid>
		<description><![CDATA[GDB is the debugging system built into Xcode. Xcode handles much of the interaction with GDB to provide support for breakpoints, stepping through/over code, wowever, GBD also provides a command line that you can use to work directly with the debugger. This tutorial walks through the basics of the command line interface along with an [...]]]></description>
			<content:encoded><![CDATA[<p>GDB is the debugging system built into Xcode. Xcode handles much of the interaction with GDB to provide support for breakpoints, stepping through/over code, wowever, GBD also provides a command line that you can use to work directly with the debugger. This tutorial walks through the basics of the command line interface along with an introduction to a handful of commands for viewing variable and object data.</p>
<p>The first thing to understand is that commands can only be entered into GDB when the debugging process is stopped, which is done via breakpoints. If you hit a breakpoint, the debug console will look as follows:<br />
<span id="more-10886"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb1.gif" alt="" title="gdb1" width="563" height="237" class="alignnone size-full wp-image-10893" /></p>
<p>Notice the <strong>(gdb)</strong> prompt, politely waiting your input. No better place to start than requesting help information. While in the debug console (and on a breakpoint), from the GDB prompt, type in &#8220;help&#8221;</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb2.gif" alt="" title="gdb2" width="560" height="287" class="alignnone size-full wp-image-10894" /></p>
<p>Once the high-level help categories are shown, you can look further by viewing one of the classes of commands. For example, below is a list of all the commands for examing data:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb3.gif" alt="" title="gdb3" width="557" height="464" class="alignnone size-full wp-image-10895" /></p>
<h5>GDB Print Command</h5>
<p>From the help info shown above, let&#8217;s look at the print command:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb4.gif" alt="" title="gdb4" width="551" height="289" class="alignnone size-full wp-image-10897" /></p>
<p>The expression (EXP) to be printed can be as simple as showing the current value of a variable &#8211; for example, using the code below, here&#8217;s how to print the value of the variable &#8216;counter&#8217;</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define MAX_CLICK  5</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>someMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>counter title<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>title message<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>msg
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>counter<span style="color: #002200;">++</span> &gt; MAX_CLICK<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    UIAlertView<span style="color: #002200;">*</span> alertView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span>title 
      message<span style="color: #002200;">:</span>msg delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span> otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alertView show<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Each time the method above stops on breakpoint at the if statement, the value of counter can be viewed:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb5.gif" alt="" title="gdb5" width="148" height="69" class="alignnone size-full wp-image-10909" /></p>
<p>Notice the shortcut for print (p) command is used above in the second command.</p>
<p>You can also use expressions when working with variables. Understand the examples that follow are a bit contrived, however, for the sake of illustration please bear with me. Notice that depending on how use define an expression, the value of the expression will vary:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb6.gif" alt="" title="gdb6" width="220" height="67" class="alignnone size-full wp-image-10903" /></p>
<h5>GDB Print-Object Command</h5>
<p>Looking at the help information for the data commands, notice there is also a print-object command, which will call the description method of an Objective-C object. Let&#8217;s assume we call <strong>someMethod</strong> (which is listed above), using the following call:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self someMethod<span style="color: #002200;">:</span>x<span style="color: #002200;">++</span> title<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error Message&quot;</span> message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Max Counter Value Reached.&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>If we were to set a breakpoint at the line <strong>if (counter++ > MAX_CLICK)</strong>, we could view the objects <strong>title</strong> and <strong>msg</strong>:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb7.gif" alt="" title="gdb7" width="203" height="71" class="alignnone size-full wp-image-10904" /></p>
<p>Notice the shortcut for print object (po) is used in the second example above.</p>
<p>Print-object is very helpful when looking at objects that are collections, for example NSDictionary and NSArray objects. We can view the following dictionary object using the print-object command:</p>

<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>arrayOfDictionaryObjects <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1234&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;2345&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;3456&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;4567&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb8.gif" alt="" title="gdb8" width="269" height="251" class="alignnone size-full wp-image-10905" /></p>
<p>At this point, even with this cursory introduction to GDB, hopefully you have a basic idea of the power of GDB. In future posts I dive further into GDB and show you how to make the most of debugging from the command interface.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-introduction-to-commands.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Objective-C Expressions for Debugging</title>
		<link>http://iPhoneDeveloperTips.com/debugging/objective-c-expressions-for-debugging.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/objective-c-expressions-for-debugging.html#comments</comments>
		<pubDate>Thu, 01 Dec 2011 12:43:41 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10633</guid>
		<description><![CDATA[In a previous post I wrote about preprocessor macros that provide filename, line number and function information to aid in debugging. A short example follows: - &#40;void&#41;buttonPressed:&#40;UIButton *&#41;button &#123; NSLog&#40;@&#34;\n Function: %s\n Pretty function: %s\n Line: %d\n File: %s\n Object: %@&#34;, __func__, __PRETTY_FUNCTION__, __LINE__, __FILE__, button&#41;; &#160; ... &#125; The output from the above: In [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://iphonedevelopertips.com/cocoa/filename-and-line-number-with-nslog-part-ii.html" target="_blank">previous post</a> I wrote about preprocessor macros that provide filename, line number and function information to aid in debugging. A short example follows:</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: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span> Function: %s<span style="color: #2400d9;">\n</span> Pretty function: %s<span style="color: #2400d9;">\n</span> Line: %d<span style="color: #2400d9;">\n</span> File: %s<span style="color: #2400d9;">\n</span> Object: %@&quot;</span>,
   __func__, __PRETTY_FUNCTION__, __LINE__, __FILE__, button<span style="color: #002200;">&#41;</span>;    
&nbsp;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><span id="more-10633"></span></p>
<p>The output from the above:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/log1.png" alt="" title="log1" width="585" height="93" class="alignnone size-full wp-image-10634" /></p>
<p>In addition to the macros, there are four Objective-C expressions which provides additional information about the current context in your code. Three of the four expressions are shown in the example below:</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: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Current selector: %@&quot;</span>, NSStringFromSelector<span style="color: #002200;">&#40;</span>_cmd<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Object class: %@&quot;</span>, NSStringFromClass<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Filename: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithUTF8String<span style="color: #002200;">:</span>__FILE__<span style="color: #002200;">&#93;</span> lastPathComponent<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/log2.gif" alt="" title="log2" width="562" height="41" class="alignnone size-full wp-image-10638" /></p>
<p>The fourth expression available in Objective-C creates an array, where each entry is a string representing a value in the current stack trace. Here is how to print the stack trace:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Stack trace: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> callStackSymbols<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/log3.gif" alt="" title="log3" width="589" height="311" class="alignnone size-full wp-image-10641" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/objective-c-expressions-for-debugging.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Xcode 4 : Debug Breakpoints, Conditions and Actions</title>
		<link>http://iPhoneDeveloperTips.com/debugging/xcode-4-debug-breakpoints-conditions-actions-and-ignore.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/xcode-4-debug-breakpoints-conditions-actions-and-ignore.html#comments</comments>
		<pubDate>Wed, 09 Nov 2011 09:11:38 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10570</guid>
		<description><![CDATA[I recently bumped into a few new debugging features in Xcode 4 while looking at breakpoint options. To show how this works, let&#8217;s look at a small block of code where I&#8217;ve already set a breakpoint by clicking in the margin area on the left side in the editor: With a breakpoint set, right click [...]]]></description>
			<content:encoded><![CDATA[<p>I recently bumped into a few new debugging features in Xcode 4 while looking at breakpoint options. To show how this works, let&#8217;s look at a small block of code where I&#8217;ve already set a breakpoint by clicking in the margin area on the left side in the editor: </p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/11/bp3.gif" alt="" title="bp3" width="474" height="290" class="alignnone size-full wp-image-10575" /><br />
<span id="more-10570"></span></p>
<p>With a breakpoint set, right click (or control-click) on the breakpoint indicator, the blue area shown below in the screenshot below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/11/bp1.gif" alt="" title="bp1" width="390" height="115" class="alignnone size-full wp-image-10571" /></p>
<p>The popup dialog below will be shown, select <strong>Edit Breakpoint</strong>:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/11/bp2.gif" alt="" title="bp2" width="392" height="118" class="alignnone size-full wp-image-10572" /></p>
<p>At this point you will be shown the dialog below. I configured the settings to only trigger the breakpoint if the value of <em>counter</em> is > 2. When this occurs, a message will be spoken that indicates the breakpoint name and hit count (how many times the breakpoint has been activated).</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/11/bp4.gif" alt="" title="bp4" width="451" height="247" class="alignnone size-full wp-image-10582" /></p>
<p>The above is just one of many options for triggering and configuring breakpoint settings. When you get a chance, I recommend you take a few minutes to get familiar with the options for managing breakpoints. And who knows, the voice response may come in handy for those times when you are looking crossed-eyed at the screen from endless hours of coding&#8230;</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/xcode-4-debug-breakpoints-conditions-actions-and-ignore.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Locating Crash Reports</title>
		<link>http://iPhoneDeveloperTips.com/debugging/locating-crash-reports.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/locating-crash-reports.html#comments</comments>
		<pubDate>Mon, 19 Sep 2011 06:21:57 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=9919</guid>
		<description><![CDATA[If an applications crashs while running on a device, iOS logs the errors and creates a crash report. The report includes specifics about the OS version, date/time, the exception type, a stack trace, among other details. Below is a partial listing of a report: Crash Reports in Xcode and Organizer If you are in the [...]]]></description>
			<content:encoded><![CDATA[<p>If an applications crashs while running on a device, iOS logs the errors and creates a crash report. The report includes specifics about the OS version, date/time, the exception type, a stack trace, among other details.</p>
<p>Below is a partial listing of a report:<br />
<span id="more-9919"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/09/crash2.png" alt="" title="Crash Log" width="514" height="336"/></a></p>
<h5>Crash Reports in Xcode and Organizer</h5>
<p>If you are in the midst of writing an application that has crashed on a development device, you can view crash reports from within Xcode Organizer:</p>
<p>- Open Organizer<br />
- Select the <strong>Devices</strong> option (on the top of the screen)<br />
- Select the device on menu (left side of the screen)<br />
- Choose <strong>Device Logs</strong></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/09/crash1.jpg" alt="" title="crash1" width="428" height="190"/></a></p>
<p>Notice the list includes all the apps that have crashed on the device, not only the applications that you&#8217;ve written.</p>
<h5>Crash Reports and the App store</h5>
<p>For applications that a user has installed from the App Store, crash logs will be uploaded to Apple and you can download using <strong>iTunes Connect</strong>. For example, if you have an application that is available on the App Store and a user has notified you that the app crashed, you can access the crash report:</p>
<p>- Log into the iOS Dev Center<br />
- From the menu in the upper right, choose iTunes Connect<br />
- Select <strong>Manage Your Applications</strong><br />
- Click on the application you are interested for which you want to see the reports<br />
- Select the <strong>View Details</strong> option below if application icon<br />
- Select the <strong>Crash Reports</strong> link</p>
<h5>Crash Reports with Ad Hoc and Enterprise Apps</h5>
<p>When a device is synchronized with iTunes, crash reports are copied from the device onto the system running iTunes. To have a user get a copy of the crash report to you, they will need to locate the report(s) from the local system and make the report available to you (email, etc).</p>
<p>The location of the report is dependent on the OS:</p>
<p><strong>Mac OS X</strong>: ~/Library/Logs/CrashReporter/MobileDevice/&lt;DEVICE_NAME&gt;</p>
<p><strong>Windows XP</strong>: C:\Documents and Settings\&lt;USERNAME&gt;\Application Data\Apple Computer\Logs\CrashReporter\MobileDevice\&lt;DEVICE_NAME&gt;</p>
<p><strong>Windows Vista or 7</strong>: C:\Users\&lt;USERNAME&gt;\AppData\Roaming\Apple Computer\Logs\CrashReporter\MobileDevice\&lt;DEVICE_NAME&gt;</p>
<p>&lt;DEVICE_NAME&gt; is the name set for the device (go to Settings &#8211; General &#8211; About)<br />
&lt;USERNAME&gt; is the name used when logging into the computer. </p>
<h5>Crash Report Files</h5>
<p>The files of the most help are those that end in <strong>.crash</strong> files. The crash report&#8217;s filename begins with the application name and contains date/time information. In addition, &lt;DEVICE_NAME&gt; will appear at the end of the file name, before the extension.</p>
<p>When locating crash log files, the files are written with the following format:</p>
<p><strong>appName_date_deviceName.crash</strong></p>
<p>For example: <strong>iPhoneApp_2011-07-25-094420_MyiPhone.crash</strong></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/locating-crash-reports.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Share and Copy Files Between an App and iTunes</title>
		<link>http://iPhoneDeveloperTips.com/debugging/using-uifilesharingenabled-for-creating-debug-log-files.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/using-uifilesharingenabled-for-creating-debug-log-files.html#comments</comments>
		<pubDate>Mon, 12 Sep 2011 07:27:50 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=9127</guid>
		<description><![CDATA[You can share files between an iOS app and your Mac using file sharing via iTunes. Using file sharing the contents of your application&#8217;s Documents directory is available in iTunes. This also works the other way, where you can place files from your Mac into the shared area of iTunes and make them available to [...]]]></description>
			<content:encoded><![CDATA[<p>You can share files between an iOS app and your Mac using file sharing via iTunes. Using file sharing the contents of your application&#8217;s <strong>Documents</strong> directory is available in iTunes. This also works the other way, where you can place files from your Mac into the shared area of iTunes and make them available to your iOS app.</p>
<p>This feature works well if you need to log information from within your app and make the log available for offline viewing at another time. I&#8217;ve used idea this a number of times to save debugging information for later viewing.<br />
<span id="more-9127"></span></p>
<h5>UIFileSharingEnabled</h5>
<p>The first step is to add the flag <strong>UIFileSharingEnabled</strong> to the application plist file. If you edit the plist as XML, you can add the flag as shown here:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UIFileSharingEnabled<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Using the plist editor, the entry is as shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/09/fileshare1.png" alt="" width="426" height="23" /></p>
<h5>Write a file to Document Directory</h5>
<p>With the property file updated, the next step is to write a file to the <strong>Documents</strong> directory within the app. The code below creates an array as well as a dictionary object and writes both containers to a file named  <strong>DictionaryContents.text</strong>.</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: #a61390;">void</span><span style="color: #002200;">&#41;</span> writeFileToDocumentDirectory
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>grains <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span> 
                    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Caramunich&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hallertauer&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Special B&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
                             grains, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrayKey&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Magnum&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hopKey&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Belgian Strong&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;yeastKey&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Get path to documents directory</span>
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, 
                                                       NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Path to save dictionary</span>
    <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>dictPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> 
                           stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DictionaryContents.text&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Write dictionary</span>
    <span style="color: #002200;">&#91;</span>dictionary writeToFile<span style="color: #002200;">:</span>dictPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;    
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Call the method above somewhere in your application to write the file to the <strong>Documents</strong> directory.</p>
<h5>iTunes File Sharing</h5>
<p>To view the file within iTunes, plugin your device and select your device in the DEVICES section on the left:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/09/fileshare3.png" alt="" width="232" height="123" /></p>
<p>Choose the <strong>Apps</strong> button on the top of iTunes and scroll to the bottom of the screen, you will see a list of all the apps that can transfer files between your computer and your device.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/09/fileshare2.png" alt="" width="590" height="170" /></p>
<p><br/><br />
Save the file <strong>DictionaryContents.txt</strong> to your system, the contents will look as follows:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>arrayKey<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Caramunich<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Hallertauer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Special B<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hopKey<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Magnum<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>yeastKey<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Belgian Strong<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h5>Copy Files To An App</h5>
<p>You can also copy files from your system to the <strong>Documents</strong> directory, making the files accessible to your app &#8211; drag/drop files onto the <strong>Sandbox Documents</strong> area.</p>
<h5>Keeping Data Private</h5>
<p>- If you need to keep application information private, do not store the files in the <strong>Documents</strong> directory. Instead, store private information in the Library directory, or a folder within that directory. With one exception, the <strong>Library</strong> directory and all its sub-directories will be preserved across application backups and updates &#8211; /Library/Caches is not saved.</p>
<p>- With file sharing, you cannot share files between applications.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/using-uifilesharingenabled-for-creating-debug-log-files.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exceptions &#8211; Try, Catch and Finally</title>
		<link>http://iPhoneDeveloperTips.com/debugging/exceptions-try-catch-and-finally.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/exceptions-try-catch-and-finally.html#comments</comments>
		<pubDate>Mon, 15 Aug 2011 09:04:02 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=9448</guid>
		<description><![CDATA[You won&#8217;t find exceptions used as frequently in Objective-C as in other languages. However, try/catch/finally blocks can be useful to capture errors that may otherwise cause an app to fail. In the example below, I allocate an array, however I don&#8217;t add any elements to the array. Inside the try block the attempt to access [...]]]></description>
			<content:encoded><![CDATA[<p>You won&#8217;t find exceptions used as frequently in Objective-C as in other languages. However, try/catch/finally blocks can be useful to capture errors that may otherwise cause an app to fail.</p>
<p>In the example below, I allocate an array, however I don&#8217;t add any elements to the array. Inside the try block the attempt to access the first element will cause an exception to be thrown.<span id="more-9448"></span></p>

<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> arraytest <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">@try</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Attempt access to an empty array</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Object: %@&quot;</span>, <span style="color: #002200;">&#91;</span>arraytest objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@catch</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSException</span> <span style="color: #002200;">*</span>exception<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Print exception information</span>
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NSException caught&quot;</span> <span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Name: %@&quot;</span>, exception.name<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Reason: %@&quot;</span>, exception.reason <span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">return</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@finally</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Cleanup, in both success and fail cases</span>
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;In finally block&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>arraytest release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The output of the code is shown below:</p>
<p><strong>iPhoneApp[4464:b903] NSException caught<br />
iPhoneApp[4464:b903] Name: NSRangeException<br />
iPhoneApp[4464:b903] Reason: *** -[NSArray objectAtIndex:]: index 0 beyond bounds for empty array<br />
iPhoneApp[4508:b903] In finally block</strong></p>
<p>The @finally clause is called regardless of the outcome of the code the in @try block. This is the perfect place to perform any relevant code cleanup.</p>
<p>Using exceptions can be helpful when working with files (for example <strong>NSFileHandleOperationException</strong>), when working with data from unknown sources, etc.</p>
<h5>Catching a Specific Exception</h5>
<p>Using <strong>NSException</strong> as shown in the @catch statement above, will result in all exceptions being caught. You can also look for a specific exception &#8211; the code below looks only for <strong>NSRangeException</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@try</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Attempt access to an empty array</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Object: %@&quot;</span>, <span style="color: #002200;">&#91;</span>arraytest objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@catch</span> <span style="color: #002200;">&#40;</span>NSRangeException <span style="color: #002200;">*</span>exception<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Print exception information</span>
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NSRangeException caught&quot;</span> <span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Reason: %@&quot;</span>, exception.reason <span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">return</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@finally</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Cleanup, in both success and fail cases</span>
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;In finally block&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>arraytest release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Throwing an Exception</h5>
<p>You can also throw an exception in your own code. Although a bit contrived (as a means to get the point across), the method <strong>testException()</strong> shown below will throw an exception whenever it is called:</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: #a61390;">void</span><span style="color: #002200;">&#41;</span>testException
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSException</span> <span style="color: #002200;">*</span>exception <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSException</span> exceptionWithName<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Exception!&quot;</span>
                                                     reason<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Something not so good occurred.&quot;</span>
                                                   userInfo<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">@throw</span> exception;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Further along in the code, when <strong>testException()</strong> is called, wrap the invocation in a try/catch/finally blocks in a manner similar to what was shown above:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@try</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Call the method...</span>
  <span style="color: #002200;">&#91;</span>self testException<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@catch</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSException</span> <span style="color: #002200;">*</span>exception<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Print exception information</span>
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NSException caught&quot;</span> <span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Name: %@&quot;</span>, exception.name<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Reason: %@&quot;</span>, exception.reason <span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">return</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@finally</span> 
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;In finally block&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>iPhoneApp[4660:b903] NSException caught<br />
iPhoneApp[4660:b903] Name: Exception!<br />
iPhoneApp[4660:b903] Reason: Something not so good occurred.<br />
iPhoneApp[4660:b903] In finally block<br />
</strong></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/exceptions-try-catch-and-finally.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Working with Assertions to Debug your Apps</title>
		<link>http://iPhoneDeveloperTips.com/debugging/working-with-assertions.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/working-with-assertions.html#comments</comments>
		<pubDate>Mon, 27 Jun 2011 11:00:57 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=8758</guid>
		<description><![CDATA[Assertions provide a mechanism to catch errors by checking a condition statement(s) and throwing an exception if the condition check fails. Assertions are implemented as macros that you can use to evaluate conditions at various points in your application. Often times assertions are used as a sanity check to verify variables contain the values, or [...]]]></description>
			<content:encoded><![CDATA[<p>Assertions provide a mechanism to catch errors by checking a condition statement(s) and throwing an exception if the condition check fails. Assertions are implemented as macros that you can use to evaluate conditions at various points in your application. Often times assertions are used as a sanity check to verify variables contain the values, or range of values expected. </p>
<p>As an example, I&#8217;ve used assertions during early development phases when working with web services &#8211; as I was writing the iPhone app another team was developing web-services and programming interfaces to the same. As we were getting parameter passing to work, assertions were very helpful to check expected values.<br />
<span id="more-8758"></span></p>
<h5>Using Assertions in Objective-C Methods</h5>
<p>Two categories of assertions are available when writing iOS apps, assertions for use within Objective-C methods, and assertions that are used inside a C-based function. Let&#8217;s start with method based assertions:</p>
<p><strong>NSAssert</strong> and its kin &#8211; NSAssert1, NSAssert2, etc &#8211; are used inside methods. <strong>NSAssert</strong> is defined as follows:</p>
<p><strong>#define NSAssert(condition, desc)</strong></p>
<p><strong>condition</strong> is the expression we are interested to evaluate and <strong>desc</strong> is the message displayed on the console when the assertion fails.</p>
<p>Looking a little further, in <strong>NSException.h</strong> we can see the full definition:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define NSAssert(condition, desc, ...) \</span>
    <span style="color: #a61390;">do</span> <span style="color: #002200;">&#123;</span>			\
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>condition<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>	\
	    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAssertionHandler</span> currentHandler<span style="color: #002200;">&#93;</span> handleFailureInMethod<span style="color: #002200;">:</span>_cmd \
		object<span style="color: #002200;">:</span>self file<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithUTF8String<span style="color: #002200;">:</span>__FILE__<span style="color: #002200;">&#93;</span> \
	    	lineNumber<span style="color: #002200;">:</span>__LINE__ description<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>desc<span style="color: #002200;">&#41;</span>, <span style="color: #6e371a;">##__VA_ARGS__]; \</span>
	<span style="color: #002200;">&#125;</span>			\
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">while</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span></pre></div></div>

<p>Notice the check for <strong>!(condition)</strong> &#8211; if the condition check fails, an exception is generated with information about the file where the error occurred and the line number within that file.</p>
<p>The following example shows how you might go about using an assertion:</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: #a61390;">void</span><span style="color: #002200;">&#41;</span> sanityCheck<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>x
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Other code here...</span>
&nbsp;
  NSAssert<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>x &lt; <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x is not within the acceptable range of 1-99&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>If the value of x is not between 1 and 99, an exception will be generated and the following output will be shown in the console:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/06/assert1.png" alt="" width="568" height="82" /></p>
<p>In addition to the basic NSAssert macro, there are macros available that enable additional information to be output into the description field. For example, the macro below takes an additional parameter, arg1, that can be used to show the value of a variable in the output:</p>
<p><strong>#define NSAssert1(condition, desc, arg1)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">NSAssert1<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>x &lt; <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x is: %i, acceptable range is: 1-99&quot;</span>, x<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output is shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/06/assert2.png" alt="" width="566" height="81" /></p>
<p>Taking this one step further, the example below shows NSAssert3, which will accept three arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define MIN_VALUE 1 </span>
<span style="color: #6e371a;">#define MAX_VALUE 99</span>
&nbsp;
NSAssert3<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>x &lt; <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x is:%i, acceptable range is:%d-%d&quot;</span>, x, MIN_VALUE, MAX_VALUE<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output is essentially the same as in the previous example, however, the values for the range as derived from the min and max value definitions.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/06/assert3.png" alt="" /></p>
<p>The entire range of macros for Objective-C methods is: NSAssert, NSAssert1, NSAssert2, NSAssert3, NSAssert4 and NSAssert5.</p>
<h5>Using Assertions in C Functions</h5>
<p>Assertions can also be used within C functions and work in a very similar fashion as with Objective-C methods. There is a range of macros with the naming conventions NSCAssert, NSCAssert1, NSCAssert2, etc. <strong>NSCAssert</strong> looks as follows:</p>
<p><strong> NSCAssert(condition, NSString *description)</strong></p>
<p>You use these macros in C functions in the same manner as shown above.</p>
<h5>Disabling Assertions</h5>
<p>I typically use assertions during development only &#8211; you can disable assertions by adding the preprocessor macro NS_BLOCK_ASSERTIONS in the build settings for your Release builds.</p>
<p>The screenshot below shows how to configure the Build Settings inside Xcode 4 to add NS_BLOCK_ASSERTIONS.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/06/assert4.png" alt="" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/working-with-assertions.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled</title>
		<link>http://iPhoneDeveloperTips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html#comments</comments>
		<pubDate>Thu, 08 Apr 2010 11:47:00 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6091</guid>
		<description><![CDATA[It&#8217;s only a matter of time before you&#8217;ll find yourself face to face with the dreaded EXC_BAD_ACCESS error message. This message generally occurs when you attempt to access an object that has previously been released. NSZombieEnabled is an environment variable which can be helpful to track down the elusive object causing the problem. With the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s only a matter of time before you&#8217;ll find yourself face to face with the dreaded EXC_BAD_ACCESS error message. This message generally occurs when you attempt to access an object that has previously been released.</p>
<p><strong>NSZombieEnabled</strong> is an environment variable which can be helpful to track down the elusive object causing the problem. With the environment variable set, Cocoa will redirect all objects <strong>isa</strong> pointer to an NZZombie object once an objects retain count has reached zero. In the future, should you try and send a message to a previously released object, an exception will be raised showing a message similar to the following:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/04/zombie1.png" /></p>
<h5>Setting NSZombieEnabled Environment Variable</h5>
<p>In the Groups and Files section, expand the <strong>Executables</strong> section and right click on your app name and choose <strong>Get Info</strong> from the dialog:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/04/zombie3.png" /></p>
<p>Select the <strong>Arguments</strong> tab on the top and then add a new entry in the <strong>Variables to be set in the environment</strong> section. Name the new variable to <strong>NSZombieEnabled</strong> and set its value to <strong>YES</strong>.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/04/zombie2.png" /></p>
<h5>Tracking Down the Error</h5>
<p>Using a zombie, messages sent to an object that has already released will now generate the exception shown above, and following the stack track should now help you find the location of the object that is generating the EXC_BAD_ACCESS error.</p>
<p>Don&#8217;t forget to remove the environment variable, or at a minimum untick the checkbox once you find the source of the problem.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

