<?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; Cocoa</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/cocoa/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>UITextView with Rounded Corners and Colored Border</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/uitextview-with-rounded-corners-and-border-color.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/uitextview-with-rounded-corners-and-border-color.html#comments</comments>
		<pubDate>Wed, 26 Jan 2011 10:04:15 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[border]]></category>
		<category><![CDATA[corner]]></category>
		<category><![CDATA[uitextview]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=7594</guid>
		<description><![CDATA[In a previous post, Add Rounded Corners and Border to UIWebview, I wrote a few lines of code to add rounded corners and a border color to a webview. A few people have inquired if this is also possible on a UITextView, the answer is yes, and it&#8217;s equally as easy to code. The image [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post, <a  target="_blank"  href="http://iphonedevelopertips.com/cocoa/add-rounded-corners-and-border-to-uiwebview.html">Add Rounded Corners and Border to UIWebview</a>, I wrote a few lines of code to add rounded corners and a border color to a webview. A few people have inquired if this is also possible on a <strong>UITextView</strong>, the answer is yes, and it&#8217;s equally as easy to code.</p>
<p>The image below shows a <strong>UITextView</strong> with rounded corners, the code follows:<br />
<span id="more-7594"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/01/uitextview-round.png" alt="" width="335" height="126" /></p>
<p>I began with a simple view controller and one textview instance variable:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> TestViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  UITextView  <span style="color: #002200;">*</span>textViewStatus;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>In the loadview method, I setup the view and create an instance of a textview. I follow this by setting the background color, font, aligning the text and toggling editable to off. </p>
<p>Lines 13-16 define the border color, width and the corner radius. Notice the code in this section references the Core Animation layer of the textview, which is the key to futzing with the radius.</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
19
20
21
</pre></td><td 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>loadView 
<span style="color: #002200;">&#123;</span>
  <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>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span><span style="color: #2400d9;">0.0</span> green<span style="color: #002200;">:</span>.34 blue<span style="color: #002200;">:</span>.74 alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  textViewStatus <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake	<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">280</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor lightGrayColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont boldSystemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">16.0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentCenter<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setEditable<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// For the border and rounded corners</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>textViewStatus layer<span style="color: #002200;">&#93;</span> setBorderColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span> CGColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>textViewStatus layer<span style="color: #002200;">&#93;</span> setBorderWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.3</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>textViewStatus layer<span style="color: #002200;">&#93;</span> setCornerRadius<span style="color: #002200;">:</span><span style="color: #2400d9;">15</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setClipsToBounds<span style="color: #002200;">:</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>textViewStatus setText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iOSDeveloperTips.com&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <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>textViewStatus<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h5>Download Source Code</h5>
<p>Xcode Project &#8211; <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/01/Xcode-RoundCornersUITextView.zip">Round Corners on UITextView</a></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/cocoa/uitextview-with-rounded-corners-and-border-color.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Obfuscate / Encrypt a String (NSString)</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/obfuscation-encryption-of-string-nsstring.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/obfuscation-encryption-of-string-nsstring.html#comments</comments>
		<pubDate>Thu, 05 Aug 2010 03:46:55 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6829</guid>
		<description><![CDATA[Apple Keychain Services offer a secure means to store sensitive information. Through the keychain, all the hardwork is managed for you to store and retrieve content. As powerful as the keychain services are, I was recently tinkering with some code to see if I could obfuscate content within an application. I had a few strings [...]]]></description>
			<content:encoded><![CDATA[<p>Apple Keychain Services offer a secure means to store sensitive information. Through the keychain, all the hardwork is managed for you to store and retrieve content. As powerful as the keychain services are, I was recently tinkering with some code to see if I could obfuscate content within an application. </p>
<p>I had a few strings defined as constants and I was interested to see if there was a painless way to store the values as obfuscated strings, and when running the application, un-obfuscate the strings and use the same within the application.<br />
<span id="more-6829"></span></p>
<h5>Using Exclusive-Or (XOR) to Obfuscate an NSString</h5>
<p>The basic concept is that for each character in a string, I XOR the value against a key value, replacing the original character with the new XOR&#8217;d character. This will create a string that is unrecognizable from the original. To get the original string back, perform the same XOR operation with the same key. The obfuscate method is shown 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: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>obfuscate<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><span style="color: #a61390;">string</span> withKey<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>key
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create data object from the string</span>
  <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Get pointer to data to obfuscate</span>
  <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>dataPtr <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">char</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>data bytes<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Get pointer to key data</span>
  <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>keyData <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">char</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>key dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span> bytes<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Points to each char in sequence in the key</span>
  <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>keyPtr <span style="color: #002200;">=</span> keyData;
  <span style="color: #a61390;">int</span> keyIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// For each character in data, xor with current value in key</span>
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; x &lt; <span style="color: #002200;">&#91;</span>data length<span style="color: #002200;">&#93;</span>; x<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Replace current character in data with </span>
    <span style="color: #11740a; font-style: italic;">// current character xor'd with current key value.</span>
    <span style="color: #11740a; font-style: italic;">// Bump each pointer to the next character</span>
    <span style="color: #002200;">*</span>dataPtr <span style="color: #002200;">=</span> <span style="color: #002200;">*</span>dataPtr<span style="color: #002200;">++</span> <span style="color: #002200;">^</span> <span style="color: #002200;">*</span>keyPtr<span style="color: #002200;">++</span>; 
&nbsp;
    <span style="color: #11740a; font-style: italic;">// If at end of key data, reset count and </span>
    <span style="color: #11740a; font-style: italic;">// set key pointer back to start of key value</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">++</span>keyIndex <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span>key length<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
      keyIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>, keyPtr <span style="color: #002200;">=</span> keyData;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span>data encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Test Obfuscation</h5>
<p>Pass in the string to obfuscate as well as a key which will provide the characters to use in the XOR process. To un-obfuscate a string, call the method with the obfuscated string and the same key.</p>
<p>Below is a short test of the obfuscation:</p>

<div class="wp_syntax"><div 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;iPhoneDeveloperTips&quot;</span>; 
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Input string:%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;		
&nbsp;
<span style="color: #11740a; font-style: italic;">// Obfuscate string</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>obfuscatedStr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self obfuscate<span style="color: #002200;">:</span>str withKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;+@$&quot;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Obfuscated string:%@&quot;</span>, obfuscatedStr<span style="color: #002200;">&#41;</span>;	
&nbsp;
<span style="color: #11740a; font-style: italic;">// Run obfuscate again to get the original string</span>
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self obfuscate<span style="color: #002200;">:</span>obfuscatedStr withKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;+@$&quot;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Final string:%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output will look as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/08/obfuscate.png" /></p>
<p><strong>Note:</strong> The resulting obfuscated string may include non-printable characters, so if you print to the console as shown above, don&#8217;t be surprised if the output contains what looks to be spaces or newline characters.</p>
<h5>Caveat</h5>
<p>For any number of reasons, including the key is stored as part of the application, this approach is by no means a substitution for real encryption. If you have sensitive data you need to protect, use the Keychain services. With that said, there are times when storing a string in a format other than clear text in your application may be of interest.</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/cocoa/obfuscation-encryption-of-string-nsstring.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Truncate a String and Append an Ellipsis, Respecting the Font Size</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comments</comments>
		<pubDate>Mon, 26 Jul 2010 11:52:06 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737</guid>
		<description><![CDATA[A number of the UI related controls will automatically truncate and append ellipsis with no effort required on your part. For example, with UILabel you can specify the linebreak mode to indicate how you would like the system to manage wrapping and truncating the label text. However, they are times when having a method to [...]]]></description>
			<content:encoded><![CDATA[<p>A number of the UI related controls will automatically truncate and append ellipsis with no effort required on your part. For example, with UILabel you can specify the linebreak mode to indicate how you would like the system to manage wrapping and truncating the label text.</p>
<p>However, they are times when having a method to truncate a string without using a UI control would be handy. In this post I&#8217;ll create a category which will add a method to the  <strong>NSString</strong> class to do just that. You can read more about working with categories in this post: <a  target="_blank"  href="http://iphonedevelopertips.com/objective-c/categories.html">Introduction to Categories</a>.<br />
<span id="more-6737"></span></p>
<h5>NSString Category</h5>
<p>Start by creating a new interface file with the name: <strong>NSString+TruncateToWidth.h</strong> &#8211; appending the method name to the class is a common naming convention when working with categories.</p>
<p>The code below defines a method that will be available to all <strong>NSString</strong> objects. The method will truncate a string to a specific width, using the font size to properly determine the string size before truncating:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>TruncateToWidth<span style="color: #002200;">&#41;</span>
&nbsp;
<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>stringByTruncatingToWidth<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>width withFont<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIFont <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>font;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>Code to Truncate NSString</h5>
<p>The implementation of the <strong>stringByTruncatingToWidth</strong> method follows, add this code to the <strong>NSString+TruncateToWidth.m</strong> file.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;NSString+TruncateToWidth.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#define ellipsis @&quot;…&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>TruncateToWidth<span style="color: #002200;">&#41;</span>
&nbsp;
<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>stringByTruncatingToWidth<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>width withFont<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIFont <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>font
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create copy that will be the returned result</span>
  <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>truncatedString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self mutableCopy<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Make sure string is longer than requested width</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self sizeWithFont<span style="color: #002200;">:</span>font<span style="color: #002200;">&#93;</span>.width &gt; width<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Accommodate for ellipsis we'll tack on the end</span>
    width <span style="color: #002200;">-=</span> <span style="color: #002200;">&#91;</span>ellipsis sizeWithFont<span style="color: #002200;">:</span>font<span style="color: #002200;">&#93;</span>.width;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Get range for last character in string</span>
    <span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span>truncatedString.length <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#125;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Loop, deleting characters until string fits within width</span>
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>truncatedString sizeWithFont<span style="color: #002200;">:</span>font<span style="color: #002200;">&#93;</span>.width &gt; width<span style="color: #002200;">&#41;</span> 
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Delete character at end</span>
      <span style="color: #002200;">&#91;</span>truncatedString deleteCharactersInRange<span style="color: #002200;">:</span>range<span style="color: #002200;">&#93;</span>;
&nbsp;
      <span style="color: #11740a; font-style: italic;">// Move back another character</span>
      range.location<span style="color: #002200;">--</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Append ellipsis</span>
    <span style="color: #002200;">&#91;</span>truncatedString replaceCharactersInRange<span style="color: #002200;">:</span>range withString<span style="color: #002200;">:</span>ellipsis<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">return</span> truncatedString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Notice that the desired final width of the string is passed in, as well as the font that will be used when calculating the size (width) of the string. The process is quite simple: change the incoming width to accommodate an ellipsis on the end of the string &#8211; loop through the string removing characters from the end until the desired width is reached &#8211; append an ellipsis onto the string.</p>
<h5>Calling the Truncate NSString Method</h5>
<p>You can now call the method <strong>stringByTruncatingToWidth:withFont:</strong> on any NSString object:</p>

<div class="wp_syntax"><div 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;Lorem ipsum dolor sit amet, consectetuer adipiscing&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Adjust the width parameter to change the final string width</span>
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>str stringByTruncatingToWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">170</span> withFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">15</span><span style="color: #002200;">&#93;</span><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>

<p>The output from above would look as follows:</p>
<p><strong>Lorem ipsum dolor sit…</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/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>NSNotification, UserInfo and Object Methods</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/nsnotification-userinfo-and-object-methods.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/nsnotification-userinfo-and-object-methods.html#comments</comments>
		<pubDate>Thu, 01 Jul 2010 11:33:55 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6450</guid>
		<description><![CDATA[When working with an NSNotification object, you&#8217;ll want to familiarize yourself the userInfo dictionary, which provides access to any additional objects that may be of interest to the receiver. Understanding the object method may also be helpful if you are using the same notification on more than one object. userInfo Dictionary Below I add an [...]]]></description>
			<content:encoded><![CDATA[<p>When working with an <strong>NSNotification</strong> object, you&#8217;ll want to familiarize yourself the <strong>userInfo</strong> dictionary, which provides access to any additional objects that may be of interest to the receiver. Understanding the <strong>object</strong> method may also be helpful if you are using the same notification on more than one object.<br />
<span id="more-6450"></span></p>
<h5>userInfo Dictionary</h5>
<p>Below I add an observer for <strong>MPMoviePlayerContentPreloadDidFinishNotification</strong>, which will send a message to the method <strong>moviePreloadDidFinish:</strong> when a <strong>MoviePlayerController</strong> object has finished playing:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">mp <span style="color: #002200;">=</span>  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MPMoviePlayerController alloc<span style="color: #002200;">&#93;</span> initWithContentURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                     selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePreloadDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
                     name<span style="color: #002200;">:</span>MPMoviePlayerContentPreloadDidFinishNotification 
                     object<span style="color: #002200;">:</span>mp<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Here&#8217;s how to access and print the <strong>userInfo</strong> dictionary from the notification object:</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> moviePlayBackDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification 
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>userInfo <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>notification userInfo<span style="color: #002200;">&#93;</span>;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MPMoviePlayerPlaybackDidFinishReasonUserInfoKey: %@&quot;</span>,
    <span style="color: #002200;">&#91;</span>userInfo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MPMoviePlayerPlaybackDidFinishReasonUserInfoKey&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The value of <strong>MoviePlayerPlaybackDidFinishReasonUserInfoKey</strong> is an <strong>NSNumber</strong> object which contains an integer value specifying reason the playback finished. The possible range of return values are defined in <strong>MPMovieFinishReason</strong>, which is an enum type as shown here:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">enum</span> <span style="color: #002200;">&#123;</span>
   MPMovieFinishReasonPlaybackEnded,
   MPMovieFinishReasonPlaybackError,
   MPMovieFinishReasonUserExited
<span style="color: #002200;">&#125;</span>;
<span style="color: #a61390;">typedef</span> NSInteger MPMovieFinishReason;</pre></div></div>

<p>Pulling all this together, you could write something similar to the following to check if the user stopped playback of the movie:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>userInfo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MPMoviePlayerPlaybackDidFinishReasonUserInfoKey&quot;</span><span style="color: #002200;">&#93;</span> intValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> MPMovieFinishReasonUserExited<span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;User stopped playback&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>object Method</h5>
<p>One more handy method within <strong>NSNotification</strong> is the method <strong>object</strong>, which will return the object (as type <strong>id</strong>) that is associated with the notification. For example, check out the code below that sets up the notification, pay attention to the last parameter <strong>mp</strong> which is assigned to the <strong>object</strong> parameter.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">mp <span style="color: #002200;">=</span>  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MPMoviePlayerController alloc<span style="color: #002200;">&#93;</span> initWithContentURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                     selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePreloadDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
                     name<span style="color: #002200;">:</span>MPMoviePlayerContentPreloadDidFinishNotification 
                     object<span style="color: #002200;">:</span>mp<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Within the method called by the selector, you can now access the object passed in by querying the <strong>object</strong> method:</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> moviePlayBackDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification 
<span style="color: #002200;">&#123;</span>
  MPMoviePlayerController <span style="color: #002200;">*</span>mpObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>MPMoviePlayerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>notification object<span style="color: #002200;">&#93;</span>; 
...</pre></div></div>

<p><strong>mpObject</strong> will point to the <strong>mp</strong> object defined in the previous code block &#8211; using the <strong>object</strong> method you can retrieve a pointer to the object that made the original notification request.</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/cocoa/nsnotification-userinfo-and-object-methods.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Add Rounded Corners and Border to UIWebview</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/add-rounded-corners-and-border-to-uiwebview.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/add-rounded-corners-and-border-to-uiwebview.html#comments</comments>
		<pubDate>Mon, 21 Jun 2010 11:45:21 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6380</guid>
		<description><![CDATA[Beyond simply displaying websites, UIWebview can be a nice alternative when you need to display formatted text, not to mention the option of incorporating JavaScript which presents some very interesting scripting and UI opportunities. I&#8217;ve found webviews handy for presenting help information, displaying HTML downloaded from remote servers as well as integrating custom ad banners [...]]]></description>
			<content:encoded><![CDATA[<p>Beyond simply displaying websites, <strong>UIWebview</strong> can be a nice alternative when you need to display formatted text, not to mention the option of incorporating JavaScript which presents some very interesting scripting and UI opportunities. I&#8217;ve found webviews handy for presenting help information, displaying HTML downloaded from remote servers as well as integrating custom ad banners from open source ad servers such as <a  target="_blank"  href="http://www.openx.org/">Openx</a>.</p>
<p>This tip shows how to add rounded corners and a colored border to a <strong>UIWebview</strong>, which adds a nice touch to an otherwise squared-off look.<br />
<span id="more-6380"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/06/webview.png" /></p>
<h5>Rounded Corners on UIWebview</h5>
<p> What follows is an example of a webview that displays HTML from a file in the application bundle. The trick is to access the <strong>CALayer</strong> of the view and set the corner radius as well as the border width and color:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// This is required in order to access the CALayer properties</span>
<span style="color: #6e371a;">#import &lt;QuartzCore/QuartzCore.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create UIWebview</span>
UIWebView <span style="color: #002200;">*</span>webView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWebView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span>, <span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">310</span>, <span style="color: #2400d9;">220</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Round corners using CALayer property</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>webView layer<span style="color: #002200;">&#93;</span> setCornerRadius<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>webView setClipsToBounds<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create colored border using CALayer property</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>webView layer<span style="color: #002200;">&#93;</span> setBorderColor<span style="color: #002200;">:</span>
   <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span><span style="color: #2400d9;">0.52</span> green<span style="color: #002200;">:</span><span style="color: #2400d9;">0.09</span> blue<span style="color: #002200;">:</span><span style="color: #2400d9;">0.07</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> CGColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>webView layer<span style="color: #002200;">&#93;</span> setBorderWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.75</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<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>webView<span style="color: #002200;">&#93;</span>;
&nbsp;
...
&nbsp;
<span style="color: #002200;">&#91;</span>webView release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Note, cornerRadius and borderColor became available with the release of iPhone OS 3.0.</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/cocoa/add-rounded-corners-and-border-to-uiwebview.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Formatting Numbers &#8211; NSNumberFormatter Examples</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/formatting-numbers-nsnumberformatter-examples.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/formatting-numbers-nsnumberformatter-examples.html#comments</comments>
		<pubDate>Wed, 16 Jun 2010 11:32:19 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6337</guid>
		<description><![CDATA[The previous post on how to Get Total and Free Space on the Mounted File System is a good segue to this post, as the formatting of the output from the previous code example left a little to be desired, see the figure below. Without delimiters to mark off thousands, it&#8217;s a little difficult to [...]]]></description>
			<content:encoded><![CDATA[<p>The previous post on how to <a href="http://iphonedevelopertips.com/data-file-management/get-total-and-free-space-on-the-mounted-file-system.html" target="_blank">Get Total and Free Space on the Mounted File System</a> is a good segue to this post, as the formatting of the output from the previous code example left a little to be desired, see the figure below.</p>
<p><img style="border: 1px solid black;" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/06/format1a.png" alt="" width="251" height="27" /></p>
<p>Without delimiters to mark off thousands, it&#8217;s a little difficult to read. Welcome <strong>NSNumberFormatter</strong>, which provides a simple means to comma delimit NSNumber objects.<br />
<span id="more-6337"></span></p>
<h5>NSNumberFormatter and Thousands Separator</h5>
<p><strong>NSNumberFormatter</strong> includes a handful of pre-defined number format styles. The code below uses the decimal formatting style to provide the comma delimited format we are after:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Get access to a file manager as our means</span>
<span style="color: #11740a; font-style: italic;">// to perform file operations</span>
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Using the application home directory, get dictionary of attributes</span>
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>attributesDict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager attributesOfFileSystemForPath<span style="color: #002200;">:</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Print total file system size and available space</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;System size: %lld&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemSize<span style="color: #002200;">&#93;</span> longLongValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;System free space: %lld&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemFreeSize<span style="color: #002200;">&#93;</span> longLongValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;    
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create formatter</span>
<span style="color: #400080;">NSNumberFormatter</span> <span style="color: #002200;">*</span>formatter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumberFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;  
&nbsp;
<span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// Set to decimal style and output to console</span>
<span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #002200;">&#91;</span>formatter setNumberStyle<span style="color: #002200;">:</span>NSNumberFormatterDecimalStyle<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>formattedOutput <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>formatter stringFromNumber<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemFreeSize<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;System free space: %@&quot;</span>, formattedOutput<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output of the code above is not much more readable:</p>
<p><img style="border: 1px solid black;" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/06/format1b.png" alt="" width="252" height="18" /></p>
<h5>NSNumberFormatter and Currency</h5>
<p>Here&#8217;s another example of a style, formatting the output as currency. Note in the example that I&#8217;ve also changed the locale to demonstrate how the formatter manages output based on this setting.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// Format style as currency, output to console</span>
<span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #002200;">&#91;</span>formatter setNumberStyle<span style="color: #002200;">:</span>NSNumberFormatterCurrencyStyle<span style="color: #002200;">&#93;</span>;
formattedOutput <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>formatter stringFromNumber<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemFreeSize<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Output as currency: %@&quot;</span>, formattedOutput<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// Change local and output as currency</span>
<span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #400080;">NSLocale</span> <span style="color: #002200;">*</span>locale <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> alloc<span style="color: #002200;">&#93;</span> initWithLocaleIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;it_IT&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>formatter setLocale<span style="color: #002200;">:</span>locale<span style="color: #002200;">&#93;</span>;
formattedOutput <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>formatter stringFromNumber<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemFreeSize<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Output as currency - locale it_IT: %@&quot;</span>, formattedOutput<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output is now shown in the current locale (US) as well as the it_IT locale:</p>
<p><img style="border: 1px solid black;" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/06/format2.png" alt="" width="402" height="32" /></p>
<h5>NSNumberFormatter and Percentage</h5>
<p>The example below shows the output for percentage style output. In this case, it shows the percentage of disk space available, using the system attributes dictionary defined above.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// Format style as percentage, output to console</span>
<span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #002200;">&#91;</span>formatter setNumberStyle<span style="color: #002200;">:</span>NSNumberFormatterPercentStyle<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Set to the current locale</span>
<span style="color: #002200;">&#91;</span>formatter setLocale<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSLocale</span> currentLocale<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get percentage of system space that is available</span>
<span style="color: #a61390;">float</span> percent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemFreeSize<span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">/</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemSize<span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>num <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>percent<span style="color: #002200;">&#93;</span>;
formattedOutput <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>formatter stringFromNumber<span style="color: #002200;">:</span>num<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Percentage of system space available: %@&quot;</span>, formattedOutput<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><img style="border: 1px solid black;" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/06/format3.png" alt="" width="298" height="20" /></p>
<h5>NSNumberFormatter and Spell-Out Numbers</h5>
<p>The final example shows how to use the spell-out style.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// Format style as &quot;spell-out&quot; output to console</span>
<span style="color: #11740a; font-style: italic;">//--------------------------------------------</span>
<span style="color: #002200;">&#91;</span>formatter setNumberStyle<span style="color: #002200;">:</span>NSNumberFormatterSpellOutStyle<span style="color: #002200;">&#93;</span>;
formattedOutput <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>formatter stringFromNumber<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemFreeSize<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Spell out: %@&quot;</span>, formattedOutput<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Clean up</span>
<span style="color: #002200;">&#91;</span>formatter release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>locale release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><img style="border: 1px solid black;" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/06/format4.png" alt="" width="428" height="46" /></p>
<h5>Share Your NSNumberFormatter Examples</h5>
<p>I&#8217;m sure there are any number of interesting ways to leverage this class. If you have a formatter style that you&#8217;ve found helpful, please post a comment with a working code example.</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/cocoa/formatting-numbers-nsnumberformatter-examples.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Overriding NSObject Description Method</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/overriding-nsobject-description-method.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/overriding-nsobject-description-method.html#comments</comments>
		<pubDate>Mon, 29 Mar 2010 07:03:38 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5933</guid>
		<description><![CDATA[No doubt NSLog is your friend, I use it frequently to print messages to the console during development. Actually, I use a variation of the NSLog that prints messages to the console sans date, time and object information, you can read about my approach here: Yet Another Debug Output (NSLog Replacement) In this tip I&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>No doubt NSLog is your friend, I use it frequently to print messages to the console during development. Actually, I use a variation of the NSLog that prints messages to the console sans date, time and object information, you can read about my approach here: <a href="http://iphonedevelopertips.com/debugging/yet-another-debug-output-nslog-replacement.html">Yet Another Debug Output (NSLog Replacement)</a></p>
<p>In this tip I&#8217;ll cover something similar as it relates to printing debug information to the console, specifically, overriding the description method of an object to provide information about instance variables.<br />
<span id="more-5933"></span></p>
<h5> NSObject and description method</h5>
<p>All objects inherit from <strong>NSObject</strong>, which exposes a method named <strong>description</strong>. By default this method prints the object name and its address in memory.</p>
<p>For example, let&#8217;s say we have an object that looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> testAppObject <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>beerName;   <span style="color: #11740a; font-style: italic;">// Homebrew name</span>
  <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>beerStyle;  <span style="color: #11740a; font-style: italic;">// Character and origin</span>
  <span style="color: #a61390;">float</span>     beerIBU;     <span style="color: #11740a; font-style: italic;">// Bitterness range</span>
  <span style="color: #400080;">NSArray</span>   <span style="color: #002200;">*</span>arrayHops;  <span style="color: #11740a; font-style: italic;">// Types of hops</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Somewhere in our code we initialize the object values to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">...
beerStyle <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;American Ale&quot;</span><span style="color: #002200;">&#93;</span>;
beerName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hoppy Times&quot;</span><span style="color: #002200;">&#93;</span>;
beerIBU <span style="color: #002200;">=</span> <span style="color: #2400d9;">63.90</span>;
arrayHops <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;Amarillo&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cascade&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Centennial&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Chinook&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
...</pre></div></div>

<p>Now, let&#8217;s create an instance of that object and use NSLog to print the object to the console:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">testAppObject <span style="color: #002200;">*</span>obj;
&nbsp;
obj <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>testAppObject alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, obj<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/03/desc1.png" /></p>
<p>When NSLog prints the value of the object, it calls the description method, however, the information is a little underwhelming. To its credit, you can call NSLog with other objects such as arrays, dictionaries, etc to see their values. However, requesting the description for an object does not provide specifics on the instance variables within the object.</p>
<h5>Override description method</h5>
<p>We can override the <strong>description</strong> method in <strong>NSObject</strong> to provide more specifics about a class.</p>
<p>If we add the following method to <strong>testAppObject</strong> class, overriding the description method in NSObject, we can print information for each instance variable, including the array:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>description
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">return</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;<span style="color: #2400d9;">\n</span>Beer style: %@ <span style="color: #2400d9;">\n</span>Beer name: %@<span style="color: #2400d9;">\n</span>IBUs = %3.2f
            <span style="color: #2400d9;">\n</span>Categories: %@&quot;</span>, beerStyle, beerName, beerIBU, arrayHops<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The output for NSLog will now look as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/03/desc2.png" /></p>
<p>You may find this helpful when you need to see the value of all instance variables in one shot.</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/cocoa/overriding-nsobject-description-method.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Storing CGPoint, CGSize and CGRect in Collections with NSValue</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/storing-cgpoint-cgsize-and-cgrect-in-collections-with-nsvalue.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/storing-cgpoint-cgsize-and-cgrect-in-collections-with-nsvalue.html#comments</comments>
		<pubDate>Wed, 17 Mar 2010 07:06:35 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5823</guid>
		<description><![CDATA[In an earlier post CGRect, CGSize and CGPoint Functions I demonstrated a number of geometry structures available for representing a point (CGPoint &#8211; x and y coordinates), size (CGSize &#8211; height and width) and rectangles (CGRect &#8211; combination of both). Unfortunately, you cannot directly store any of the above in a collection, for example an [...]]]></description>
			<content:encoded><![CDATA[<p>In an earlier post <a href="http://iphonedevelopertips.com/c/cgrect-cgsize-and-cgpoint-functions.html">CGRect, CGSize and CGPoint Functions</a> I demonstrated a number of geometry structures available for representing a point (CGPoint &#8211; x and y coordinates), size (CGSize &#8211; height and width) and rectangles (CGRect &#8211; combination of both).</p>
<p>Unfortunately, you cannot directly store any of the above in a collection, for example an array, as each of these is a C structure, not an object.<br />
<span id="more-5823"></span></p>
<h5>NSValue Object</h5>
<p>No worry, NSValue to the rescue. This object can not only hold the structures mentioned above, it can also hold scalar types such as integers,  floating point numbers, characters, etc. And of course, NSValue objects can be stored within collections.</p>
<p>The code that follows will create CGRect, CGPoint and CGRect structures, corresponding NSValue objects for each, and store the later into an NSArray. The code will also reconstitute the original structures from the NSValue objects.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// CGPoint converted to NSValue</span>
CGPoint point <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">9</span>, <span style="color: #2400d9;">9</span><span style="color: #002200;">&#41;</span>;
<span style="color: #400080;">NSValue</span> <span style="color: #002200;">*</span>pointObj <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSValue</span> valueWithCGPoint<span style="color: #002200;">:</span>point<span style="color: #002200;">&#93;</span>;		
&nbsp;
<span style="color: #11740a; font-style: italic;">// CGSize converted to NSValue</span>
CGSize size <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100</span>, <span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span>;
<span style="color: #400080;">NSValue</span> <span style="color: #002200;">*</span>sizeObj <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSValue</span> valueWithCGSize<span style="color: #002200;">:</span>size<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// CGRect from CGPoint and CGSize converted to NSValue</span>
CGRect rect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>point.x, point.y, size.width, size.height<span style="color: #002200;">&#41;</span>;
<span style="color: #400080;">NSValue</span> <span style="color: #002200;">*</span>rectObj <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSValue</span> valueWithCGRect<span style="color: #002200;">:</span>rect<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Add the objects to a collection</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>pointObj, sizeObj, rectObj, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Print to console the objects in the collection</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array content: %@&quot;</span>, array<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Restore from NSValue to C structures</span>
CGPoint pointRestored <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pointObj CGPointValue<span style="color: #002200;">&#93;</span>;
CGSize sizeRestored <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>sizeObj CGSizeValue<span style="color: #002200;">&#93;</span>;
CGRect rectRestored <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>rectObj CGRectValue<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Print restored values</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;point x:%f y:%f&quot;</span>, pointRestored.x, pointRestored.y<span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;size  w:%f h:%f&quot;</span>, sizeRestored.width, sizeRestored.height<span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rect x:%f y:%f w:%f h:%f&quot;</span>, rectRestored.origin.x, rectRestored.origin.y, 
  rectRestored.size.width, rectRestored.size.height<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output from the NSLog statements is below:</p>
<p><img src="http://androiddevelopertips.com/wp-content/uploads/2010/03/nsvalue.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/cocoa/storing-cgpoint-cgsize-and-cgrect-in-collections-with-nsvalue.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Get Application Icon Name</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/get-application-icon-name.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/get-application-icon-name.html#comments</comments>
		<pubDate>Thu, 18 Feb 2010 03:35:31 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5511</guid>
		<description><![CDATA[In a previous post, Get Application Name, I wrote a line of code to get the application name from the app bundle. The line of code below is a slight modification that shows how to get the name that will appear on the iPhone below the icon: &#91;&#91;&#91;NSBundle mainBundle&#93; infoDictionary&#93; objectForKey:@&#34;CFBundleDisplayName&#34;&#93;; The CFBundleDisplayName value corresponds [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post, <a  target="_blank"  href="http://iphonedevelopertips.com/cocoa/get-application-name.html">Get Application Name</a>, I wrote a line of code to get the application name from the app bundle.</p>
<p>The line of code below is a slight modification that shows how to get the name that will appear on the iPhone below the icon:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> infoDictionary<span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CFBundleDisplayName&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The <strong>CFBundleDisplayName</strong> value corresponds to the Bundle Display Name in the application plist file, see the figure below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/02/icon2.png" alt="" title="icon2" width="490" height="239" class="alignnone size-full wp-image-5514" /></p>
<p>You can see the application icon for this example in this figure:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/02/icon3.png" alt="" title="icon3" width="329" height="130" class="alignnone size-full wp-image-5521" /></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/cocoa/get-application-icon-name.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSRange and NSString Objects</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/nsrange-and-nsstring-objects.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/nsrange-and-nsstring-objects.html#comments</comments>
		<pubDate>Thu, 24 Dec 2009 17:52:12 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5132</guid>
		<description><![CDATA[When poking around NSString methods you&#8217;ll find many references to NSRange, which is nothing more than a C structure that is helpful for describing a series of items, including a starting location and a count. For example, a range is helpful to extract a substring from another string, where you specify the starting location and [...]]]></description>
			<content:encoded><![CDATA[<p>When poking around <strong>NSString</strong> methods you&#8217;ll find many references to <strong>NSRange</strong>, which is nothing more than a C structure that is helpful for describing a series of items, including a starting location and a count. For example, a range is helpful to extract a substring from another string, where you specify the starting location and number of elements needed (examples to follow). </p>
<h5>NSRange Definition</h5>
<p>NSRange is a structure defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> _NSRange 
<span style="color: #002200;">&#123;</span>
  NSUInteger location;
  NSUInteger length;
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">NSRange</span>;</pre></div></div>

<p><span id="more-5132"></span></p>
<p><strong>location</strong> is the starting index in the range (zero based) and <strong>length</strong> is the number of entries in the range. <strong>NSUInteger</strong> is simply an unsigned value that supports both 32 and 64 bit systems. Here is how <strong>NSUInteger</strong> is defined:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">long</span> NSUInteger;
<span style="color: #6e371a;">#else</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> NSUInteger;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<h5>NSRange and Strings</h5>
<p>The example below shows one approach for creating a range and using the same to extract a substring &#8211; the output from below is <strong><em>IPA</em></strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Starting at position 25, get 3 characters</span>
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> NSMakeRange <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">25</span>, <span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// This would also work:</span>
<span style="color: #11740a; font-style: italic;">// NSRange range = {25, 3};</span>
&nbsp;
NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Beer shortname: %@&quot;</span>, <span style="color: #002200;">&#91;</span>homebrew substringWithRange<span style="color: #002200;">:</span>range<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>If you want to search for a substring, you could write something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>homebrew rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Did we find the string &quot;IPA&quot; ?</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>range.length &gt; <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;Range is: %@&quot;</span>, NSStringFromRange<span style="color: #002200;">&#40;</span>range<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output from above will display: <strong><em>Range is: {25, 3}</em></strong>. Notice the call to <strong>NSStringFromRange()</strong> which will display the return value (a range) as an <strong>NSString</strong>. There is also a function to create a range from a string: <strong>NSRangeFromString()</strong>.</p>
<p>Let&#8217;s look at one more example, the code below will search for the string &#8220;ia&#8221; starting at the end of the string moving towards the beginning:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Search for the &quot;ia&quot; starting at the end of string</span>
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>homebrew rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ia&quot;</span> options<span style="color: #002200;">:</span>NSBackwardsSearch<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// What did we find</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>range.length &gt; <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;Range is: %@&quot;</span>, NSStringFromRange<span style="color: #002200;">&#40;</span>range<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The result from above is: <strong><em>Range is: {12, 2}</em></strong> (the &#8220;ia&#8221; inside the word &#8220;India&#8221;).</p>
<h5>NSRange Functions</h5>
<p>Here is a list of functions that work with ranges:</p>
<p>NSEqualRanges()<br />
NSIntersectionRange()<br />
NSLocationInRange()<br />
NSMakeRange()<br />
NSMaxRange()<br />
NSRangeFromString()<br />
NSStringFromRange()<br />
NSUnionRange()</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/cocoa/nsrange-and-nsstring-objects.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

