<?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; C</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/c/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>Converting Between C and Objective-C Strings (NSString)</title>
		<link>http://iPhoneDeveloperTips.com/c/converting-between-c-and-objective-c-strings.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/converting-between-c-and-objective-c-strings.html#comments</comments>
		<pubDate>Mon, 07 Feb 2011 13:23:11 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=7726</guid>
		<description><![CDATA[Given the foundation of Objective-C is built upon the C programming language, it&#8217;s good to know how to convert C based strings to NSString objects, and vice-versa. For clarification: A C string is simply a series of characters (a one-dimensional character array) that is null-terminated, whereas an NSString object is a complete object with class [...]]]></description>
			<content:encoded><![CDATA[<p>Given the foundation of Objective-C is built upon the C programming language, it&#8217;s good to know how to convert C based strings to <strong>NSString</strong> objects, and vice-versa. </p>
<p>For clarification: A C string is simply a series of characters (a one-dimensional character array) that is null-terminated, whereas an <strong>NSString</strong> object is a complete object with class methods, instance methods, etc.<br />
<span id="more-7726"></span></p>
<p>The first code block below shows how to create an <strong>NSString</strong> object and convert it to a C string. The second example  shows a C string converted to an <strong>NSString</strong> object.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// NSString object to C</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;String object to a C string&quot;</span>;
<span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>ptr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>str cStringUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%s<span style="color: #2400d9;">\n</span>&quot;</span>, ptr<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Another approach with the same result</span>
<span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>ptr2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>str UTF8String<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%s<span style="color: #2400d9;">\n</span>&quot;</span>, ptr2<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// C string to NSString object</span>
ptr <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;C string to string object&quot;</span>;
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span>ptr encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<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/c/converting-between-c-and-objective-c-strings.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Using #error and #warning Compiler Directives</title>
		<link>http://iPhoneDeveloperTips.com/c/using-error-and-warning-compiler-directives.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/using-error-and-warning-compiler-directives.html#comments</comments>
		<pubDate>Tue, 23 Mar 2010 02:23:08 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5929</guid>
		<description><![CDATA[Although not your everyday directives, #error and #warning definitely have there place. Let&#8217;s take a short look at a few examples where you might find these directives helpful. #error When the preprocessor runs into the #error directive, it will write an error to the console and generate a compile time error. You can also include [...]]]></description>
			<content:encoded><![CDATA[<p>Although not your everyday directives, #error and #warning definitely have there place. Let&#8217;s take a short look at a few examples where you might find these directives helpful.</p>
<h5>#error</h5>
<p>When the preprocessor runs into the #error directive, it will write an error to the console and generate a compile time error. You can also include a text string which is displayed as an error message.<br />
<span id="more-5929"></span></p>
<p>For example, when writing a previous blog post about using <a href="http://iphonedevelopertips.com/networking/iphone-json-flickr-tutorial-part-1.html">JSON and the Flickr API</a> , I needed a way to flag developers who downloaded the source code to insert their own Flickr ID into the application code. I did this by using the following code which generated a compile time error and displayed a message to the console informing the user what was up.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#error : Change this value to your Flickr key</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">const</span> FlickrAPIKey <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;your-key-here&quot;</span>;</pre></div></div>

<p>Another time this can be helpful is when you need to verify that some particular value has been defined for the application. As a simple example, let&#8217;s say the our application needs to be flagged as either a paid or free app &#8211; we could write something similar to the following as a verification this step has been completed:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Validate the application type has been set</span>
<span style="color: #6e371a;">#ifdef PAID_APP</span>
  <span style="color: #11740a; font-style: italic;">// Do something</span>
<span style="color: #6e371a;">#elif FREE_APP</span>
  <span style="color: #11740a; font-style: italic;">// Do something else</span>
<span style="color: #6e371a;">#else</span>
  <span style="color: #6e371a;">#error : No App type defined</span>
<span style="color: #6e371a;">#endif`</span></pre></div></div>

<p>When I write the error message notice that I insert a &#8220;:&#8221; character as it helps to break apart the error message. Also worth mentioning is that the preprocessor will work through all the source code files before the compiler stops, which is nice in that you can have more than one #error message. The figure below shows error messages from two separate source files:</p>
<p><img src="http://iphonedevelopertips.com/wp-content/uploads/2010/03/error.png" /></p>
<p>As a final example, I&#8217;ve also used this as a personal marker in a block of code so that I could recall where I was and what I was working on when setting something aside. This is nice if you need to walk away from a project for some time and want an unavoidable reminder of where you left off.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#error : Check this code first</span></pre></div></div>

<h5>#warning</h5>
<p>The directive #warning is similar however the preprocessor doesn&#8217;t stop when it finds this directive, it simply displays the message to the console and keeps on with the task at hand. One common use of #warning is to display information regarding deprecated code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#warning : This method is deprecated, use someNewMethod instead</span></pre></div></div>

<p><strong>Note:</strong><br />
You can optionally surround the #error and #warning messages in quotes.</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/c/using-error-and-warning-compiler-directives.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Comment Out Large Blocks of Code with Nested Comments</title>
		<link>http://iPhoneDeveloperTips.com/c/comment-out-large-blocks-of-code-with-nested-comments.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/comment-out-large-blocks-of-code-with-nested-comments.html#comments</comments>
		<pubDate>Thu, 18 Mar 2010 13:53:01 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5904</guid>
		<description><![CDATA[In working with a large chunk of code recently, I needed to comment out a sizeable block to verify something was working as expected. My first thought was to simply to embed the offending code within /* and */ /* // Detect touch anywhere UITouch *touch = [touches anyObject]; &#160; // Where is the point [...]]]></description>
			<content:encoded><![CDATA[<p>In working with a large chunk of code recently, I needed to comment out a sizeable block to verify something was working as expected. My first thought was to simply to embed the offending code within /* and */<br />
<span id="more-5904"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
  // Detect touch anywhere
  UITouch *touch = [touches anyObject];
&nbsp;
  // Where is the point touched
  CGPoint point = [touch locationInView:self.view]; 
  NSLog(@&quot;pointx: %f pointy:%f&quot;, point.x, point.y);
&nbsp;
  // Was a tab touched, if so, which one...
  if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point))
	  NSLog(@&quot;tab 1 touched&quot;);
  else if (...)
    ... more code here
  else if (...)
    ... more code here
&nbsp;
*/</span></pre></div></div>

<p>This approach works well, with one exception, nested comments in the form of /* &#8230; */ will generate a compiler error, see lines 9 &#8211; 10 below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*
  // Detect touch anywhere
  UITouch *touch = [touches anyObject];
&nbsp;
  // Where is the point touched
  CGPoint point = [touch locationInView:self.view]; 
  NSLog(@&quot;pointx: %f pointy:%f&quot;, point.x, point.y);
&nbsp;
  /* If you have an embedded comment in this style,
       you will get a compiler error */</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<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;tab 1 touched&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>...<span style="color: #002200;">&#41;</span>
    ... more code here
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>...<span style="color: #002200;">&#41;</span>
    ... more code here
&nbsp;
<span style="color: #002200;">*/</span></pre></td></tr></table></div>

<p>To get around the problem with embedded comments, you can try this approach:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#if 0</span>
  <span style="color: #11740a; font-style: italic;">// Detect touch anywhere</span>
  UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Where is the point touched</span>
  CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>; 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pointx: %f pointy:%f&quot;</span>, point.x, point.y<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">/* If you have an embedded comment in this style,
       you will get a compiler error */</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<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;tab 1 touched&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>...<span style="color: #002200;">&#41;</span>
    ... more code here
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>...<span style="color: #002200;">&#41;</span>
    ... more code here
&nbsp;
<span style="color: #6e371a;">#endif</span></pre></td></tr></table></div>

<p>Another option that is a little more descriptive:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#ifdef VISIBLE</span>
  <span style="color: #11740a; font-style: italic;">// Detect touch anywhere</span>
  UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Where is the point touched</span>
  CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>; 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pointx: %f pointy:%f&quot;</span>, point.x, point.y<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">/* If you have an embedded comment in this style,
       you will get a compiler error */</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<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;tab 1 touched&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>...<span style="color: #002200;">&#41;</span>
    ... more code here
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>...<span style="color: #002200;">&#41;</span>
    ... more code here
&nbsp;
<span style="color: #6e371a;">#endif</span></pre></td></tr></table></div>

<p>This approach provides a means to quickly comment in/out any number of code blocks by simply modifying a compiler directive as shown here:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Code above will be compiled</span>
<span style="color: #11740a; font-style: italic;">// Change to INVISIBLE and code will ~not~ be compiled</span>
<span style="color: #6e371a;">#define VISIBLE</span></pre></div></div>

<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/c/comment-out-large-blocks-of-code-with-nested-comments.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C Conditional Operator aka Ternary Operator</title>
		<link>http://iPhoneDeveloperTips.com/c/c-conditional-operator-aka-ternary-operator.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/c-conditional-operator-aka-ternary-operator.html#comments</comments>
		<pubDate>Tue, 12 Jan 2010 02:57:50 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5244</guid>
		<description><![CDATA[I&#8217;ve always been fond of the conditional operator in C, which is essentially a terse way to write an if/else statement. Given this operator works with three operands, it is often fondly referred to as the ternary operator. The conditional looks as follow: condition-test ? first-expression : second-expression If the condition-test is nonzero, the first-expression [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always been fond of the conditional operator in C, which is essentially a terse way to write an if/else statement. Given this operator works with three operands, it is often fondly referred to as the ternary operator.</p>
<p>The conditional looks as follow:</p>
<p><strong>condition-test ? first-expression : second-expression<br />
</strong><br />
If the condition-test is nonzero, the first-expression is evaluated, otherwise the second-expression is evaluated.<br />
<span id="more-5244"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Traditional approach</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span>;
<span style="color: #a61390;">else</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Conditional operator</span>
str <span style="color: #002200;">=</span> x &gt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span>;</pre></div></div>

<h5>Nesting Conditional Operators</h5>
<p>Extending the example above, we can update the code for the case where x is neither positive or negative (x is zero), using nested conditionals:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Traditional approach</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span>;
<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>x &lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span>;
<span style="color: #a61390;">else</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;zero&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Conditional operator</span>
str <span style="color: #002200;">=</span> x &gt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span> <span style="color: #002200;">:</span> x &lt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;zero&quot;</span>;</pre></div></div>

<p>As shown above, based on the association of parameters, no parenthesis are necessary, however, I find this much easier to read:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">str <span style="color: #002200;">=</span> x &gt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span> <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>x &lt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;zero&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>Good Use Case</h5>
<p>I find the conditional operator useful when creating messages in which part of the message may be plural or singular depending on the value of a variable. </p>
<p>It&#8217;s not much more work to make messages appear as expected, for example, how often have you seen a message from an application that looks similar to this &#8220;<strong><em>1 file(s) downloaded</em></strong>&#8221; where ideally the message would read &#8220;<strong><em>1 file downloaded</em></strong>&#8221; &#8211; not a big deal, however, it&#8217;s a trivial amount of code to get this right:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> 
  <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d file%s downloaded&quot;</span>, x, <span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">1</span> ? <span style="color: #bf1d1a;">&quot;s&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Sometimes it&#8217;s the little things that will setup you apart in a crowd of developers.</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/c/c-conditional-operator-aka-ternary-operator.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Getting a Random Number</title>
		<link>http://iPhoneDeveloperTips.com/c/getting-a-random-number.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/getting-a-random-number.html#comments</comments>
		<pubDate>Wed, 22 Jul 2009 11:04:27 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2781</guid>
		<description><![CDATA[If you need to generate a random number within your iPhone application, you have to push aside Objective-C, as there is not a class with a method for generating random numbers (as in Java). The alternative is to use C, among the functions available are: rand(), srand(), random(), srandom() and arc4random(). arc4random() tends to be [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to generate a random number within your iPhone application, you have to push aside Objective-C, as there is not a class with a method for generating random numbers (as in Java). The alternative is to use C, among the functions available are: <strong>rand()</strong>, <strong>srand()</strong>, <strong>random()</strong>, <strong>srandom()</strong> and <strong>arc4random()</strong>.</p>
<p><strong>arc4random()</strong> tends to be preferred as it does not require seeding, the function automatically initializes itself.<br />
<span id="more-2781"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Get random value between 0 and 99</span>
<span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> arc4random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">%</span> <span style="color: #2400d9;">100</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get random number between 500 and 1000</span>
<span style="color: #a61390;">int</span> y <span style="color: #002200;">=</span>  <span style="color: #002200;">&#40;</span>arc4random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">%</span> <span style="color: #2400d9;">501</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">+</span> <span style="color: #2400d9;">500</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<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/c/getting-a-random-number.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Toggle an Integer Between 1 and 0</title>
		<link>http://iPhoneDeveloperTips.com/c/toggle-an-integer-between-1-and-0.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/toggle-an-integer-between-1-and-0.html#comments</comments>
		<pubDate>Tue, 28 Apr 2009 20:08:27 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2157</guid>
		<description><![CDATA[If you ever come upon a need to toggle an integer value between 1 and 0, consider using the bitwise exclusive-OR (^) operator in C to get the job done. In a recent application I wrote a method with one parameter, an integer, that is expected to be 1 or 0. In creating a demo [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever come upon a need to toggle an integer value between 1 and 0, consider using the bitwise exclusive-OR (<strong>^</strong>) operator in C to get the job done.</p>
<p>In a recent application I wrote a method with one parameter, an integer, that is expected to be 1 or 0. In creating a demo of the application I wanted to pass in alternating values of 1 and 0 as part of a test for a specific use case. Instead of using an if statement in the calling method to decide when to send a 1 or 0, I wrote something similar to the code below:<br />
<span id="more-2157"></span></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>someMethod
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">static</span> <span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Toggles 1 to 0 and 0 to 1 using xor</span>
  x <span style="color: #002200;">^=</span> <span style="color: #2400d9;">1</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Call the method that expects a 1 or 0</span>
  <span style="color: #002200;">&#91;</span>methodCallHere toggle<span style="color: #002200;">:</span>x<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>More often than not, exclusive-OR (^), AND (&#038;) and inclusive-OR (|) are consider a bit-level operations (see this post on <a href="http://iphonedevelopertips.com/c/working-with-bitfields.html">bitfields</a>). However, there are times when these operators work equally well with integer values.</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/c/toggle-an-integer-between-1-and-0.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Working with Bitfields</title>
		<link>http://iPhoneDeveloperTips.com/c/working-with-bitfields.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/working-with-bitfields.html#comments</comments>
		<pubDate>Mon, 22 Dec 2008 08:25:38 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1373</guid>
		<description><![CDATA[Given space is often at a premium when writing applications for mobile devices, I want to show you how to use bitfields to manage a series of values that need only on/off status. The upside is that you can store a surprising number of status values within a single integer, 32 to be exact. The [...]]]></description>
			<content:encoded><![CDATA[<p>Given space is often at a premium when writing applications for mobile devices, I want to show you how to use bitfields to manage a series of values that need only on/off status. The upside is that you can store a surprising number of status values within a single integer, 32 to be exact. </p>
<p>The code that follows is pure C. Given that Objective-C is a superset of C, don&#8217;t forget that you can leverage all that C has to offer, beyond working with objects.<br />
<span id="more-1373"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">unsigned</span> preLoaded<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> saveState<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> saveLoginName<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span> statusFlags;</pre></td></tr></table></div>

<p>I&#8217;ve defines three bitfields, all which will be saved within an unsigned integer. Accessing the bits is as easy as this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Setting bits</span>
statusFlags.preLoaded <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
statusFlags.saveState <span style="color: #002200;">=</span> statusFlags.saveLoginName <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get bits</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>statusFlags.saveLoginName <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  ... <span style="color: #a61390;">do</span> something here...</pre></td></tr></table></div>

<p>Using the sizeof() function, let&#8217;s compare the size of an integer to the size of the structure we created:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Setting bits</span>
statusFlags.preLoaded <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
statusFlags.saveState <span style="color: #002200;">=</span> statusFlags.saveLoginName <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get bits</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>statusFlags.saveLoginName <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;saveLoginName bit is on.&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;saveLoginName bit is off.&quot;</span><span style="color: #002200;">&#41;</span>;  
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sizeof int: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</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;Sizeof statusFlags: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>statusFlags<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>The output looks as follows, which verifies that the bitfields are maintained within a single unsigned integer.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/12/bit1.png"/></p>
<p>What&#8217;s interesting is that we now know that an integer is 4 bytes, this would imply that we can store up to 32 separate flags within our structure. To verify this the case, let&#8217;s create a new structure that looks as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">unsigned</span> preLoaded<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> saveState<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> saveLoginName<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #a61390;">unsigned</span> buffer<span style="color: #002200;">:</span> <span style="color: #2400d9;">28</span>;
  <span style="color: #a61390;">unsigned</span> savePassword<span style="color: #002200;">:</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span> statusFlagsExtended;</pre></td></tr></table></div>

<p>In this structure, &#8216;buffer&#8217; is nothing more than a means for me to soak up 28 bits such that I can add one more bit on the end. Let&#8217;s take a look at the sizeof this structure with the code below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Setting bits</span>
statusFlags.preLoaded <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
statusFlags.saveState <span style="color: #002200;">=</span> statusFlags.saveLoginName <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get bits</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>statusFlags.saveLoginName <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;saveLoginName bit is on.&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;saveLoginName bit is off.&quot;</span><span style="color: #002200;">&#41;</span>;  
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sizeof int: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</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;Sizeof statusFlags: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>statusFlags<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;Sizeof statusFlagsExtended: %d&quot;</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>statusFlagsExtended<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>As expected, the structure &#8216;statusFlagsExtended&#8217; is also stored as an integer.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/12/bit2.png"/></p>
<p>Even thought the bulk of developing iPhone application is all about Objective-C, don&#8217;t forget to lean on C as needed to get the job done.</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/c/working-with-bitfields.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>CGRect, CGSize and CGPoint Functions</title>
		<link>http://iPhoneDeveloperTips.com/c/cgrect-cgsize-and-cgpoint-functions.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/cgrect-cgsize-and-cgpoint-functions.html#comments</comments>
		<pubDate>Fri, 07 Nov 2008 07:08:55 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[cgpoint]]></category>
		<category><![CDATA[cgrect]]></category>
		<category><![CDATA[cgsize]]></category>
		<category><![CDATA[structures]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=973</guid>
		<description><![CDATA[With an understanding of C structures, and the definitions of CGRect, CGSize and CGPoint behind us, let&#8217;s look at a handful of functions for working with these structures. CGRectMake and NSStringFromCGRect With CGRectMake we can create a new CGRect structure. The rectangles below have differing starting points, however, each have a width and height of [...]]]></description>
			<content:encoded><![CDATA[<p>With an understanding of <a target="_blank" href="http://iphonedevelopertips.com/c/c-structures.html">C structures</a>, and the <a  target="_blank" href="http://iphonedevelopertips.com/c/cgrect-cgsize-and-cgpoint.html">definitions of CGRect, CGSize and CGPoint</a> behind us, let&#8217;s look at a handful of functions for working with these structures.<br />
<span id="more-973"></span></p>
<p><strong>CGRectMake and NSStringFromCGRect</strong></p>
<p>With CGRectMake we can create a new CGRect structure. The rectangles below have differing starting points, however, each have a width and height of 100. The function NSStringFromCGRect returns a string object that defines the rectangle passed in as a parameter:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">CGRect rect1 <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>;
CGRect rect2 <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">190</span>, <span style="color: #2400d9;">190</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rect1: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect1<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;rect2: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect2<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><strong><em>Note</em> </strong> : The output of all examples are shown in the image at the bottom of this post.</p>
<p><strong>CGRectIntersect</strong></p>
<p>To determine if two rectangles intersect, you can write code as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">CGRect rect3 <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>;
CGRect rect4 <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">190</span>, <span style="color: #2400d9;">190</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectIntersectsRect<span style="color: #002200;">&#40;</span>rect3, rect4<span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;The rectangles intersect&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;The rectangles do not intersect&quot;</span><span style="color: #002200;">&#41;</span>;    
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rect3: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect3<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;rect4: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect4<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>CGRectInset</strong></p>
<p>If you need to create a rectangle that is either larger or smaller than an existing rectangle, centered on the same point, try CGRectInset:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">CGRect rect5 <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// Use positive values for a smaller rectangle</span>
CGRect rect6 <span style="color: #002200;">=</span> CGRectInset<span style="color: #002200;">&#40;</span>rect, <span style="color: #2400d9;">25</span>, <span style="color: #2400d9;">25</span><span style="color: #002200;">&#41;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%rect5: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect5<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;%rect6: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect6<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
...
&nbsp;
CGRect rect7 <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// Use negative values for a larger rectangle</span>
CGRect rect8 <span style="color: #002200;">=</span> CGRectInset<span style="color: #002200;">&#40;</span>rect, <span style="color: #002200;">-</span><span style="color: #2400d9;">25</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">25</span><span style="color: #002200;">&#41;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rect7: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect7<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;rect8: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect8<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>CGRectFromString</strong></p>
<p>A little more uncommon, is creating rectangles from a string, however, if the need arises:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;{{0,0},{50,50}}&quot;</span>;
CGRect rect9 <span style="color: #002200;">=</span> CGRectFromString<span style="color: #002200;">&#40;</span>str<span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rect9: %@&quot;</span>, NSStringFromCGRect<span style="color: #002200;">&#40;</span>rect9<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Functions for CGSize and CGPoint</strong></p>
<p>As you would expect, there are similar functions to those above for working with CGSize and CGPoint:</p>
<ul>
<li>CGSizeMake</li>
<li>CGSizeEqualToSize</li>
<li>CGPointMake</li>
<li>CGPointEqualToPoint</li>
</ul>
<p>The output for the above code examples follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/11/rects.png"/></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/c/cgrect-cgsize-and-cgpoint-functions.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>CGRect, CGSize and CGPoint</title>
		<link>http://iPhoneDeveloperTips.com/c/cgrect-cgsize-and-cgpoint.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/cgrect-cgsize-and-cgpoint.html#comments</comments>
		<pubDate>Wed, 05 Nov 2008 18:00:31 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[cgpoint]]></category>
		<category><![CDATA[cgrect]]></category>
		<category><![CDATA[cgsize]]></category>
		<category><![CDATA[structures]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=964</guid>
		<description><![CDATA[Digging into development of iPhone applications, you&#8217;ll eventually encounter references to CGRect, CGSize, and CGPoint. These references are to C structures (see this post for more information on structures). This post will provide a high-level view of what comprises CGRect and its counterparts. Here is how CGRect is defined: 1 2 3 4 5 struct [...]]]></description>
			<content:encoded><![CDATA[<p>Digging into development of iPhone applications, you&#8217;ll eventually encounter references to CGRect, CGSize, and CGPoint. These references are to C structures (<a target="_blank" href="http://iphonedevelopertips.com/c/c-structures.html">see this post</a> for more information on structures). This post will provide a high-level view of what comprises CGRect and its counterparts. Here is how CGRect is defined:<br />
<span id="more-964"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> CGRect <span style="color: #002200;">&#123;</span>
   CGPoint origin;
   CGSize size;
<span style="color: #002200;">&#125;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> CGRect CGRect;</pre></td></tr></table></div>

<p>Going a little further, we can find that CGPoint and CGSize are defined as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> CGPoint <span style="color: #002200;">&#123;</span>
   CGFloat x;
   CGFloat y;
<span style="color: #002200;">&#125;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> CGPoint CGPoint;
&nbsp;
<span style="color: #a61390;">struct</span> CGSize <span style="color: #002200;">&#123;</span>
   CGFloat width;
   CGFloat height;
<span style="color: #002200;">&#125;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> CGSize CGSize;</pre></td></tr></table></div>

<p>And to wrap up, CGFloat looks as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> <span style="color: #a61390;">float</span> CGFloat;    <span style="color: #11740a; font-style: italic;">// 32-bit</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">double</span> CGFloat; <span style="color: #11740a; font-style: italic;">// 64-bit</span></pre></td></tr></table></div>

<p>Here&#8217;s what we can summarize, a CGRect defines a rectangle as an origin (x and y location) and its size (width and height), with all values represented as floats. Access to the member variables of these structures  (and all structure members for that matter) is through dot syntax as shown in the example below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">  CGRect bounds <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> bounds<span style="color: #002200;">&#93;</span>;
  CGRect appframe<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>;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mainScreen bounds: %.0f, %.0f, %3.0f, %3.0f&quot;</span>, 
     bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height<span style="color: #002200;">&#41;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mainScreen applicationFrame: %.0f, %.0f, %3.0f, %3.0f&quot;</span>, 
     appframe.origin.x, appframe.origin.y, appframe.size.width, appframe.size.height<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>In an upcoming post I&#8217;ll look into some of the functions that are available to us when working with the above structures.</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/c/cgrect-cgsize-and-cgpoint.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>C Structures</title>
		<link>http://iPhoneDeveloperTips.com/c/c-structures.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/c-structures.html#comments</comments>
		<pubDate>Mon, 03 Nov 2008 15:40:48 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[structures]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=955</guid>
		<description><![CDATA[Leading up to a post on working with CGRect, CGPoint and CGSize, it makes sense to visit C structures. A structure is a collection of variables, grouped together to facilitate organization of data. For example, one might define a set of x and y coordinates as follows: Creating a Structure 1 2 3 4 struct [...]]]></description>
			<content:encoded><![CDATA[<p>Leading up to a post on working with CGRect, CGPoint and CGSize, it makes sense to visit C structures. A structure is a collection of variables, grouped together to facilitate organization of data. For example, one might define a set of x and y coordinates as follows:<br />
<span id="more-955"></span></p>
<p><strong>Creating a Structure</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> point<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">int</span> x;
  <span style="color: #a61390;">int</span> y;
<span style="color: #002200;">&#125;</span>;</pre></td></tr></table></div>

<p>With this definition in place, we can create new instances of a point as shown below. Also, notice how structure members are accessed using dot syntax.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Allocate, then initialize</span>
<span style="color: #a61390;">struct</span> point topLeft;
topLeft.x <span style="color: #002200;">=</span> <span style="color: #2400d9;">15</span>, topLeft.y <span style="color: #002200;">=</span> <span style="color: #2400d9;">20</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Allocate and initialize in one step</span>
<span style="color: #a61390;">struct</span> point lowerRight <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">200</span><span style="color: #002200;">&#125;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x: %d y: %d&quot;</span>, topLeft.x, topLeft.y<span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x: %d y: %d&quot;</span>, lowerRight.x, lowerRight.y<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><img style="max-width: 800px;" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/11/struct1.png" /></p>
<p><strong>Typedef</strong></p>
<p>We can add a typedef (type definition) to create a more convenient data type name. Look at the code that follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> point<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">int</span> x;
  <span style="color: #a61390;">int</span> y;
<span style="color: #002200;">&#125;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> point POINT;</pre></td></tr></table></div>

<p>Now, we can create new points as shown here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Allocate, then initialize</span>
POINT topLeft;
topLeft.x <span style="color: #002200;">=</span> <span style="color: #2400d9;">15</span>, topLeft.y <span style="color: #002200;">=</span> <span style="color: #2400d9;">20</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Allocate and initialize in one step</span>
POINT lowerRight <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">200</span><span style="color: #002200;">&#125;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x: %d y: %d&quot;</span>, topLeft.x, topLeft.y<span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x: %d y: %d&quot;</span>, lowerRight.x, lowerRight.y<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><img style="max-width: 800px;" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/11/struct2.png" /></p>
<p>It&#8217;s important to note, a typedef is nothing more than a name for an existing type, there is no new data type created.</p>
<p><strong>Nested Structures</strong></p>
<p>We can also nest structures, below I create a definition for a rectangle, by using two points:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> point<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">int</span> x;
  <span style="color: #a61390;">int</span> y;
<span style="color: #002200;">&#125;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> point POINT;
&nbsp;
<span style="color: #a61390;">struct</span> rectangle <span style="color: #002200;">&#123;</span>
  POINT topLeft;
  POINT lowerRight;
<span style="color: #002200;">&#125;</span>;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> rectangle RECTANGLE;</pre></td></tr></table></div>

<p>Here&#8217;s how one might use the rectangle structure.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">RECTANGLE box <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">200</span>, <span style="color: #2400d9;">300</span><span style="color: #002200;">&#125;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x: %d y: %d&quot;</span>, box.topLeft.x, box.topLeft.y<span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;x: %d y: %d&quot;</span>, box.lowerRight.x, box.lowerRight.y<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><img style="max-width: 800px;" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2008/11/struct3.png" /></p>
<p>That should cover what we need to look a little further into the structures CGRect, CGPoint and CGSize in another post.</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/c/c-structures.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

