<?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>A2 Apps &#187; iOS Snippets</title>
	<atom:link href="http://a2apps.com.au/category/ios-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://a2apps.com.au</link>
	<description></description>
	<lastBuildDate>Sun, 10 Mar 2013 08:52:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Remove leading and trailing spaces</title>
		<link>http://a2apps.com.au/remove-leading-and-trailing-spaces/</link>
		<comments>http://a2apps.com.au/remove-leading-and-trailing-spaces/#comments</comments>
		<pubDate>Sun, 10 Mar 2013 08:52:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[NSString]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=111</guid>
		<description><![CDATA[Removing start and end spaces of an NSString is very simple:]]></description>
				<content:encoded><![CDATA[<p>Removing start and end spaces of an NSString is very simple:</p>
<pre class="brush: objc; title: ; notranslate">

NSString* result = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

</pre>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/remove-leading-and-trailing-spaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting an integer in a string into an NSNumber</title>
		<link>http://a2apps.com.au/converting-an-integer-in-a-string-into-an-nsnumber/</link>
		<comments>http://a2apps.com.au/converting-an-integer-in-a-string-into-an-nsnumber/#comments</comments>
		<pubDate>Sun, 10 Mar 2013 02:14:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[NSNumber]]></category>
		<category><![CDATA[NSString]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=108</guid>
		<description><![CDATA[This new idiom is concise and clean:]]></description>
				<content:encoded><![CDATA[<p>This new idiom is concise and clean:</p>
<pre class="brush: objc; title: ; notranslate">
NSNumber* intNumber = @([str intValue]);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/converting-an-integer-in-a-string-into-an-nsnumber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Synchronous dispatch without deadlocking</title>
		<link>http://a2apps.com.au/synchronous-dispatch-without-deadlocking/</link>
		<comments>http://a2apps.com.au/synchronous-dispatch-without-deadlocking/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 03:59:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[GCD]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=104</guid>
		<description><![CDATA[Dispatches synchronously without deadlocking if the target thread is the current thread.]]></description>
				<content:encoded><![CDATA[<p>Dispatches synchronously without deadlocking if the target thread is the current thread.</p>
<pre class="brush: objc; title: ; notranslate">

static inline void dispatch_sync2(dispatch_queue_t queue, dispatch_block_t block) {
 if (dispatch_get_current_queue() == queue) {
 block();
 } else {
 dispatch_sync(queue, block);
 }
}

</pre>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/synchronous-dispatch-without-deadlocking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSMutableString to NSString</title>
		<link>http://a2apps.com.au/nsmutablestring-to-nsstring/</link>
		<comments>http://a2apps.com.au/nsmutablestring-to-nsstring/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 01:24:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[NSMutableString]]></category>
		<category><![CDATA[NSString]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=101</guid>
		<description><![CDATA[Method 1, making a copy: Method 2, also making a copy: Method 3, passing the original mutable string as an NSString by casting (almost never used):]]></description>
				<content:encoded><![CDATA[<p>Method 1, making a copy:</p>
<pre class="brush: objc; title: ; notranslate">

NSString* str = [mutableStr copy];

</pre>
<p>Method 2, also making a copy:</p>
<pre class="brush: objc; title: ; notranslate">

NSString* str = [NSString stringWithString:mutableStr];

</pre>
<p>Method 3, passing the original mutable string as an NSString by casting (almost never used):</p>
<pre class="brush: objc; title: ; notranslate">

NSString* immutableRefToMutableString = (NSString*)mutableStr;

</pre>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/nsmutablestring-to-nsstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deleting all entities</title>
		<link>http://a2apps.com.au/deleting-all-entities/</link>
		<comments>http://a2apps.com.au/deleting-all-entities/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 13:23:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[Core Data]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=98</guid>
		<description><![CDATA[The following code simply fetches and deletes all objects of a given entity name &#8211; just remember to save the changes afterwards.]]></description>
				<content:encoded><![CDATA[<p>The following code simply fetches and deletes all objects of a given entity name &#8211; just remember to save the changes afterwards.</p>
<pre class="brush: objc; title: ; notranslate">

+ (void)deleteAllEntities:(NSString*)name inContext:(NSManagedObjectContext*)context;
{
  NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
  [fetchRequest setEntity:[NSEntityDescription entityForName:name inManagedObjectContext:context]];
  NSArray* fetchResult = [context executeFetchRequest:fetchRequest error:nil];
  for (id obj in fetchResult) {
    [context deleteObject:obj];
  }
}

</pre>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/deleting-all-entities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easily testing if a string is nil or empty</title>
		<link>http://a2apps.com.au/easily-testing-if-a-string-is-nil-or-empty/</link>
		<comments>http://a2apps.com.au/easily-testing-if-a-string-is-nil-or-empty/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 13:16:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[Idioms]]></category>
		<category><![CDATA[NSString]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=95</guid>
		<description><![CDATA[A very common idiom: Works because calling a method on nil is not an error &#8211; just silently returns nil, which is also false.]]></description>
				<content:encoded><![CDATA[<p>A very common idiom:</p>
<pre class="brush: objc; title: ; notranslate">

if ([string length]) {

// It's a string of at least one char, and is not null.

}
</pre>
<p>Works because calling a method on nil is not an error &#8211; just silently returns nil, which is also false.</p>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/easily-testing-if-a-string-is-nil-or-empty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running code on the UI thread</title>
		<link>http://a2apps.com.au/running-code-on-the-ui-thread/</link>
		<comments>http://a2apps.com.au/running-code-on-the-ui-thread/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 13:10:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[GCD]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=89</guid>
		<description><![CDATA[As an iOS developer you&#8217;ll get to see this all the time, since you have to be enormously careful when running code that does any UI processing; such code must always run in the main (UI) thread, so when running on a different thread it&#8217;s necessary to pass a block of code to be executed <a class="read-more-link" href="http://a2apps.com.au/running-code-on-the-ui-thread/"><br />...read more</a>]]></description>
				<content:encoded><![CDATA[<p>As an iOS developer you&#8217;ll get to see this all the time, since you have to be enormously careful when running code that does any UI processing; such code must always run in the main (UI) thread, so when running on a different thread it&#8217;s necessary to pass a block of code to be executed by the main thread:</p>
<pre class="brush: objc; title: ; notranslate">
dispatch_async(dispatch_get_main_queue(), ^{

// ... code to execute in main thread ...

});
</pre>
<p>dispatch_async() will run that code right away, asynchronously &#8211; ie, it will not wait to queue the code into the main queue. Use dispatch_sync() if you want to wait until it&#8217;s done &#8211; but be careful! If you target the current queue, it will result in a deadlock. More information <a href="https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html">in the official documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/running-code-on-the-ui-thread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Helper method to convert an NSDate to string</title>
		<link>http://a2apps.com.au/helper-method-to-convert-an-nsdate-to-string/</link>
		<comments>http://a2apps.com.au/helper-method-to-convert-an-nsdate-to-string/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 12:58:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[NSDate]]></category>
		<category><![CDATA[NSDateFormatter]]></category>
		<category><![CDATA[NSString]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=83</guid>
		<description><![CDATA[For performance reasons we only create the formatter object once, and just reuse it afterwards. The formatter style can be easily tailored by duplicating the method and changing its options; we keep the method&#8217;s name as descriptive as possible of the conversion to be done. It is also a good idea to keep this in <a class="read-more-link" href="http://a2apps.com.au/helper-method-to-convert-an-nsdate-to-string/"><br />...read more</a>]]></description>
				<content:encoded><![CDATA[<p>For performance reasons we only create the formatter object once, and just reuse it afterwards. The formatter style can be easily tailored by duplicating the method and changing its options; we keep the method&#8217;s name as descriptive as possible of the conversion to be done. It is also a good idea to keep this in a category on NSString.</p>
<pre class="brush: objc; title: ; notranslate">

+ (NSString*)dateAsFullDateTime:(NSDate*)date;
{
  static NSDateFormatter * formatter = nil;
  if (formatter == nil) {
    formatter = [[NSDateFormatter  alloc] init];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    [formatter setTimeStyle:NSDateFormatterLongStyle];
    [formatter setDoesRelativeDateFormatting:NO];
  }
  return [formatter stringFromDate:date];
}

</pre>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/helper-method-to-convert-an-nsdate-to-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>URL Encoding an NSString</title>
		<link>http://a2apps.com.au/url-encoding-an-nsstring/</link>
		<comments>http://a2apps.com.au/url-encoding-an-nsstring/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 12:53:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[NSString]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=78</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<pre class="brush: objc; title: ; notranslate">

+ (NSString*)urlEncodeString:(NSString*)str;

{

return (__bridge_transfer NSString *)

CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL,
   (CFStringRef)@&quot;!*'();:@&amp;=+$,/?%#[ ]&quot;, kCFStringEncodingUTF8);

}

</pre>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/url-encoding-an-nsstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Proper thread-safe singleton</title>
		<link>http://a2apps.com.au/proper-thread-safe-singleton/</link>
		<comments>http://a2apps.com.au/proper-thread-safe-singleton/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 12:26:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS Snippets]]></category>
		<category><![CDATA[Idioms]]></category>

		<guid isPermaLink="false">http://a2apps.com.au/?p=65</guid>
		<description><![CDATA[The following is a correct thread-safe implementation of the singleton +sharedInstance method, which relies on GCD:]]></description>
				<content:encoded><![CDATA[<p>The following is a correct thread-safe implementation of the singleton +sharedInstance method, which relies on GCD:</p>
<pre class="brush: objc; title: ; notranslate">

+ (id)sharedInstance;
{
 static id sharedInstance = nil;
 static dispatch_once_t once = 0;
 dispatch_once(&amp;once,
 ^{ sharedInstance = [[self alloc] init]; }
 );
 return sharedInstance;
}

</pre>
]]></content:encoded>
			<wfw:commentRss>http://a2apps.com.au/proper-thread-safe-singleton/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
