<?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>Sorted Bits &#187; Coding</title>
	<atom:link href="http://www.sortedbits.com/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sortedbits.com</link>
	<description>objectiveC, C# and more interesting stuff</description>
	<lastBuildDate>Wed, 04 Jan 2012 10:11:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Async downloading of data</title>
		<link>http://www.sortedbits.com/async-downloading-of-data/</link>
		<comments>http://www.sortedbits.com/async-downloading-of-data/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 08:07:49 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.sortedbits.com/?p=1573</guid>
		<description><![CDATA[This code shows you HOW to achieve something and it is not intended as a copy-paste solution. My post about using a NSURLConnection in a different thread has been read a lot. I made this post almost 3 years ago and I would like to show you [...]]]></description>
			<content:encoded><![CDATA[<div class="warning">
<div class="msg-box-icon pngfix">This code shows you HOW to achieve something and it is not intended as a copy-paste solution.</div>
</div>
<p>My post about using a <a href="http://www.sortedbits.com/nsurlconnection-in-its-own-thread">NSURLConnection in a different thread</a> has been read a lot. I made this post almost 3 years ago and I would like to show you how I would solve it nowadays. In the last 3 years I have learned a lot and the cocoa-touch framework evolved too.</p>
<p>Between that post and now I have been trying out frameworks that support async operations and after using <a href="http://allseeing-i.com/ASIHTTPRequest/">ASIHTTPRequest</a> for a long time, it was time to move on (the project was discontinued). After trying out several more, I came across <a href="https://github.com/lukeredpath/LRResty">LRResty</a>, which is a nice framework designed with <a href="http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html">code-blocks</a> in mind.</p>
<p>Here is a small example of a piece of code, which is taken directly form the <a href="http://projects.lukeredpath.co.uk/resty/documentation.html">LRResty documentation page</a>:</p>
<pre class="syntax c">
[[LRResty client] get:@&quot;http://www.example.com&quot;
            withBlock:^(LRRestyResponse *response) {

    if(response.status == 200) {
      NSLog(@&quot;Successful response %@&quot;, [response asString]);
    }
}];
</pre>
<p>This piece of code fetches the content of an URL (www.example.com) and when the response comes back, it checks if the response is OK (status code 200). When it is, it does a simple NSLog.</p>
<p>Here you can see the power of code-blocks already. You define the way it handles a responses directly when you do the request, in my opinion this keeps code clean and simple.</p>
<p>Now we can take this a step further and see what happens if we launch this in a new thread. For this example I am taking a piece of code of an actual library I was writing some time ago, it involves getting some data through the GitHub API, waiting for the response and then executing a certain piece of code, while this can be a bit overwhelming at first, it sure beats writing dozens of extra methods for each response.</p>
<p>Because we are going the be launching the request from another thread, we have to take <a href="http://en.wikipedia.org/wiki/Race_condition">race conditions</a> into account, this was a bug that hit me when I was first using code-blocks and multi-threading.</p>
<p>I created a method which can be called with a single NSDictionary as parameter, containing code-blocks and other stuff which it needs to perform the request. Because the GitHub API needs authentication also, I will also show how that works. The dictionary contains 3 objects with the keys &#8216;success&#8217;, &#8216;failed&#8217; and &#8216;url&#8217;.</p>
<p>The objects linked to the &#8216;success&#8217; and &#8216;failed&#8217; keys are code-blocks, I will show the contents later. </p>
<pre class="syntax c">
// This method performs the actual request and can be called in a different thread.
- (void) startRequest:(NSDictionary*) parameters
{
// Take the URL out of the dictionary and store it in a NSString object.
	NSString* url = [parameters objectForKey:@&quot;url&quot;];

// Get both the 'success' and 'failed' code-blocks from the dictionary
	void(^success)(LRRestyResponse*) = [parameters objectForKey:@&quot;success&quot;];
	void(^failed)(LRRestyResponse*) = [parameters objectForKey:@&quot;failed&quot;];

// Check if there already is a client active in this class and if not, check if authentication is set.
// If not, just raise an exception for now.
	if (client == nil &amp;&amp; self.username == nil &amp;&amp; self.password == nil)
	{
		[NSException raise:@&quot;Authentication missing&quot; format:@&quot;Username and/or password not set.&quot;];
	}

// No client is predefined and should be instantiated with the right credentials.
	if (client == nil)
		self.client = [LRResty authenticatedClientWithUsername:username password:password];

	NSLog(@&quot;Requesting data from: %@&quot;, url);

	[client get:url withBlock:^(LRRestyResponse* response) {
// Grab the headers of the response
		NSDictionary* dict = response.headers;

// Check if the request was finished without errors, if there were no errors, execute our 'success' block.
// Otherwise execute our 'failed' block.
// The response is a parameter for our success and failed block.
		if ([[dict objectForKey:@&quot;Status&quot;] isEqualToString:@&quot;200 OK&quot;])
		{
			success(response);
		}
		else
		{
			failed(response);
		}
	}];
}
</pre>
<p>With this method I can perform any request <strong>I</strong> want using a different thread. I am not a 100% sure this covers it in your cases (it probably doesn&#8217;t).</p>
<p>Now, lets see the code which we use to actually call the <strong>startRequest</strong> method:</p>
<pre class="syntax c">
// This gets a page of gists using the GitHub API. When it is succesfully done it executes a supplied code-block which
// extracts the gists from the response and returns an array. When it fails it will execute a code-block
// which will show an error in the application.
- (void) getGistsForPage:(int) page success:(void(^)(NSArray*)) succesBlock failed:(void(^)(void)) failedBlock
{
// Prepare the URL for the request.
	NSString* gistsUrl = [NSString stringWithFormat:@&quot;%@%@&quot;, self.githubUrl, @&quot;/gists&quot;];

// Here I define the code to be executed when the request is completed successfully. As you can see
// it grabs an array from the response and executes the supplied 'successBlock' with that array
// as a parameter.
	void (^success)(LRRestyResponse*) = ^(LRRestyResponse* response) {
		NSArray* array = [response.responseData arrayValue];
		succesBlock(array);
	};

// This is the code that will be executed when the request fails. It simply executes the
// supplied failedBlock code.
	void (^failed)(LRRestyResponse*) = ^(LRRestyResponse* response) {
		failedBlock();
	};

// Call the startRequest method, with a dictionary which contains our succesBlock, our failedBlock and the URL
// we created earlier in this method.
	[NSThread detachNewThreadSelector:@selector(startRequest:)
							 toTarget:self
						   withObject:[NSDictionary dictionaryWithObjectsAndKeys:
									   success, @&quot;success&quot;,
									   failed, @&quot;failed&quot;,
									   gistsUrl, @&quot;url&quot;,
									   nil]];
}
</pre>
<p>It&#8217;s pretty straightforward. Now just to be complete, I will post the code I use to call the <strong>getGistsForPage</strong> method too, so you get a complete picture:</p>
<pre class="syntax c">
// Instantiate our API class, containing the methods I described above
GitHubAPI* api = [[GitHubAPI alloc] initWithURL:@&quot;https://api.github.com&quot;];

// Make sure the username, password and delegate are set
api.password = @&quot;your_password&quot;;
api.username = @&quot;your_username&quot;;
api.delegate = self;

// Call the method, which uses the multi-threaded part.
[api getGistsForPage:0 success:^(NSArray* array) {
	NSLog(@&quot;Array count: %lu&quot;, [array count]);
} failed:^(void) {
}];
</pre>
<p>That&#8217;s it, there is nothing more to it. <strong>For me</strong> this works like a charm!</p>
<p>PS. You can still use a delegate with LRResty, instead of the code-blocks. You need to implement this method when using it this way:</p>
<pre class="syntax c">
- (void)restClient:(LRRestyClient *)client receivedResponse:(LRRestyResponse *)res;
{
  // do something with the response
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/async-downloading-of-data/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Async downloading of data</title>
		<link>http://www.sortedbits.com/async-downloading-of-data/</link>
		<comments>http://www.sortedbits.com/async-downloading-of-data/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 08:07:49 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.sortedbits.com/?p=1573</guid>
		<description><![CDATA[This code shows you HOW to achieve something and it is not intended as a copy-paste solution. My post about using a NSURLConnection in a different thread has been read a lot. I made this post almost 3 years ago and I would like to show you [...]]]></description>
			<content:encoded><![CDATA[<div class="warning">
<div class="msg-box-icon pngfix">This code shows you HOW to achieve something and it is not intended as a copy-paste solution.</div>
</div>
<p>My post about using a <a href="http://www.sortedbits.com/nsurlconnection-in-its-own-thread">NSURLConnection in a different thread</a> has been read a lot. I made this post almost 3 years ago and I would like to show you how I would solve it nowadays. In the last 3 years I have learned a lot and the cocoa-touch framework evolved too.</p>
<p>Between that post and now I have been trying out frameworks that support async operations and after using <a href="http://allseeing-i.com/ASIHTTPRequest/">ASIHTTPRequest</a> for a long time, it was time to move on (the project was discontinued). After trying out several more, I came across <a href="https://github.com/lukeredpath/LRResty">LRResty</a>, which is a nice framework designed with <a href="http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html">code-blocks</a> in mind.</p>
<p>Here is a small example of a piece of code, which is taken directly form the <a href="http://projects.lukeredpath.co.uk/resty/documentation.html">LRResty documentation page</a>:</p>
<pre class="syntax c">
[[LRResty client] get:@&quot;http://www.example.com&quot;
            withBlock:^(LRRestyResponse *response) {

    if(response.status == 200) {
      NSLog(@&quot;Successful response %@&quot;, [response asString]);
    }
}];
</pre>
<p>This piece of code fetches the content of an URL (www.example.com) and when the response comes back, it checks if the response is OK (status code 200). When it is, it does a simple NSLog.</p>
<p>Here you can see the power of code-blocks already. You define the way it handles a responses directly when you do the request, in my opinion this keeps code clean and simple.</p>
<p>Now we can take this a step further and see what happens if we launch this in a new thread. For this example I am taking a piece of code of an actual library I was writing some time ago, it involves getting some data through the GitHub API, waiting for the response and then executing a certain piece of code, while this can be a bit overwhelming at first, it sure beats writing dozens of extra methods for each response.</p>
<p>Because we are going the be launching the request from another thread, we have to take <a href="http://en.wikipedia.org/wiki/Race_condition">race conditions</a> into account, this was a bug that hit me when I was first using code-blocks and multi-threading.</p>
<p>I created a method which can be called with a single NSDictionary as parameter, containing code-blocks and other stuff which it needs to perform the request. Because the GitHub API needs authentication also, I will also show how that works. The dictionary contains 3 objects with the keys &#8216;success&#8217;, &#8216;failed&#8217; and &#8216;url&#8217;.</p>
<p>The objects linked to the &#8216;success&#8217; and &#8216;failed&#8217; keys are code-blocks, I will show the contents later. </p>
<pre class="syntax c">
// This method performs the actual request and can be called in a different thread.
- (void) startRequest:(NSDictionary*) parameters
{
// Take the URL out of the dictionary and store it in a NSString object.
	NSString* url = [parameters objectForKey:@&quot;url&quot;];

// Get both the 'success' and 'failed' code-blocks from the dictionary
	void(^success)(LRRestyResponse*) = [parameters objectForKey:@&quot;success&quot;];
	void(^failed)(LRRestyResponse*) = [parameters objectForKey:@&quot;failed&quot;];

// Check if there already is a client active in this class and if not, check if authentication is set.
// If not, just raise an exception for now.
	if (client == nil &amp;&amp; self.username == nil &amp;&amp; self.password == nil)
	{
		[NSException raise:@&quot;Authentication missing&quot; format:@&quot;Username and/or password not set.&quot;];
	}

// No client is predefined and should be instantiated with the right credentials.
	if (client == nil)
		self.client = [LRResty authenticatedClientWithUsername:username password:password];

	NSLog(@&quot;Requesting data from: %@&quot;, url);

	[client get:url withBlock:^(LRRestyResponse* response) {
// Grab the headers of the response
		NSDictionary* dict = response.headers;

// Check if the request was finished without errors, if there were no errors, execute our 'success' block.
// Otherwise execute our 'failed' block.
// The response is a parameter for our success and failed block.
		if ([[dict objectForKey:@&quot;Status&quot;] isEqualToString:@&quot;200 OK&quot;])
		{
			success(response);
		}
		else
		{
			failed(response);
		}
	}];
}
</pre>
<p>With this method I can perform any request <strong>I</strong> want using a different thread. I am not a 100% sure this covers it in your cases (it probably doesn&#8217;t).</p>
<p>Now, lets see the code which we use to actually call the <strong>startRequest</strong> method:</p>
<pre class="syntax c">
// This gets a page of gists using the GitHub API. When it is succesfully done it executes a supplied code-block which
// extracts the gists from the response and returns an array. When it fails it will execute a code-block
// which will show an error in the application.
- (void) getGistsForPage:(int) page success:(void(^)(NSArray*)) succesBlock failed:(void(^)(void)) failedBlock
{
// Prepare the URL for the request.
	NSString* gistsUrl = [NSString stringWithFormat:@&quot;%@%@&quot;, self.githubUrl, @&quot;/gists&quot;];

// Here I define the code to be executed when the request is completed successfully. As you can see
// it grabs an array from the response and executes the supplied 'successBlock' with that array
// as a parameter.
	void (^success)(LRRestyResponse*) = ^(LRRestyResponse* response) {
		NSArray* array = [response.responseData arrayValue];
		succesBlock(array);
	};

// This is the code that will be executed when the request fails. It simply executes the
// supplied failedBlock code.
	void (^failed)(LRRestyResponse*) = ^(LRRestyResponse* response) {
		failedBlock();
	};

// Call the startRequest method, with a dictionary which contains our succesBlock, our failedBlock and the URL
// we created earlier in this method.
	[NSThread detachNewThreadSelector:@selector(startRequest:)
							 toTarget:self
						   withObject:[NSDictionary dictionaryWithObjectsAndKeys:
									   success, @&quot;success&quot;,
									   failed, @&quot;failed&quot;,
									   gistsUrl, @&quot;url&quot;,
									   nil]];
}
</pre>
<p>It&#8217;s pretty straightforward. Now just to be complete, I will post the code I use to call the <strong>getGistsForPage</strong> method too, so you get a complete picture:</p>
<pre class="syntax c">
// Instantiate our API class, containing the methods I described above
GitHubAPI* api = [[GitHubAPI alloc] initWithURL:@&quot;https://api.github.com&quot;];

// Make sure the username, password and delegate are set
api.password = @&quot;your_password&quot;;
api.username = @&quot;your_username&quot;;
api.delegate = self;

// Call the method, which uses the multi-threaded part.
[api getGistsForPage:0 success:^(NSArray* array) {
	NSLog(@&quot;Array count: %lu&quot;, [array count]);
} failed:^(void) {
}];
</pre>
<p>That&#8217;s it, there is nothing more to it. <strong>For me</strong> this works like a charm!</p>
<p>PS. You can still use a delegate with LRResty, instead of the code-blocks. You need to implement this method when using it this way:</p>
<pre class="syntax c">
- (void)restClient:(LRRestyClient *)client receivedResponse:(LRRestyResponse *)res;
{
  // do something with the response
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/async-downloading-of-data/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A circle progressbar</title>
		<link>http://www.sortedbits.com/a-circle-progressbar/</link>
		<comments>http://www.sortedbits.com/a-circle-progressbar/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 07:35:55 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1458</guid>
		<description><![CDATA[For a small application I wanted to write, I wanted to make a progressbar that is a circle instead of a normal horizontal bar. I wanted to press a button, which is just a normal image and then have a circle go around it, showing the progress. [...]]]></description>
			<content:encoded><![CDATA[<p>For a small application I wanted to write, I wanted to make a progressbar that is a circle instead of a normal horizontal bar. I wanted to press a button, which is just a normal image and then have a circle go around it, showing the progress.</p>
<p><a href="http://www.wim.me/wp-content/uploads/2011/11/SBCircleProgressBar.png" rel="wp-prettyPhoto[1458]"><img src="http://www.wim.me/wp-content/uploads/2011/11/SBCircleProgressBar.png" alt="" title="SBCircleProgressBar" width="218" height="184" class="alignnone size-full wp-image-1459" /></a><br />
Like you can see above in the image, it just looks awesome!</p>
<p>To achieve this, I followed a couple of simple steps:</p>
<ul>
<li>Create a cool button image, where you want the circle progressbar to appear in</li>
<li>Find out the center and radius of the circle progressbar, you can do this either by using an image editor or just guess some values and tweak them when they are not completely perfect.</li>
<li>Create a class subclassing a UIView, which has 2 properties : MaxValue (the value that is the maximum you want to use) and Progress (what is the current progress of the progressbar)</li>
<li>Put it all on another View and change the Progress property when the progress of your process changes</li>
</ul>
<div id="gist-1378588" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">//</span></div><div class='line' id='LC2'><span class="c1">//  SBCircleProgressBar.h</span></div><div class='line' id='LC3'><span class="c1">//</span></div><div class='line' id='LC4'><span class="c1">//  Created by Wim Haanstra on 17-11-11.</span></div><div class='line' id='LC5'><span class="c1">//  Copyright (c) 2011 Sorted Bits. All rights reserved.</span></div><div class='line' id='LC6'><span class="c1">//</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="k">@interface</span> <span class="nc">SBCircleProgressBar</span> : <span class="nc">UIView</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="c1">// The maximum value of the progressbar</span></div><div class='line' id='LC11'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">)</span> <span class="kt">float</span> <span class="n">MaxValue</span><span class="p">;</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="c1">// The current value of the progressbar</span></div><div class='line' id='LC14'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">)</span> <span class="kt">float</span> <span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="k">@end</span></div><div class='line' id='LC17'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1378588/02f81ee6ecdb257e2d68313ff611cfe1a21132d9/SBCircleProgressBar.h" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1378588#file_sb_circle_progress_bar.h" style="float:right;margin-right:10px;color:#666">SBCircleProgressBar.h</a>
            <a href="https://gist.github.com/1378588">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">//</span></div><div class='line' id='LC2'><span class="cp">//  SBCircleProgressBar.m</span></div><div class='line' id='LC3'><span class="cp">//</span></div><div class='line' id='LC4'><span class="cp">//  Created by Wim Haanstra on 17-11-11.</span></div><div class='line' id='LC5'><span class="cp">//  Copyright (c) 2011 Sorted Bits. All rights reserved.</span></div><div class='line' id='LC6'><span class="cp">//</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="cp">#import &quot;SBCircleProgressBar.h&quot;</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="k">@implementation</span> <span class="nc">SBCircleProgressBar</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'><span class="k">@synthesize</span> <span class="n">MaxValue</span><span class="p">,</span> <span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="cp">// SOME VALUES YOU CAN CHANGE TO CHANGE THE BEHAVIOUR OF THE PROGRESSBAR</span></div><div class='line' id='LC15'><span class="cp">#define MAX_DEGREES	360</span></div><div class='line' id='LC16'><span class="cp">#define START_DEGREES	270</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="cp">// Define the radius that the circle needs</span></div><div class='line' id='LC19'><span class="cp">#define CIRCLE_RADIUS	27</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'><span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">initWithFrame:</span><span class="p">(</span><span class="n">CGRect</span><span class="p">)</span><span class="nv">frame</span></div><div class='line' id='LC22'><span class="p">{</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">self</span> <span class="o">=</span> <span class="p">[</span><span class="n">super</span> <span class="nl">initWithFrame:</span><span class="n">frame</span><span class="p">];</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC25'><br/></div><div class='line' id='LC26'>		<span class="n">self</span><span class="p">.</span><span class="n">backgroundColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="n">clearColor</span><span class="p">];</span></div><div class='line' id='LC27'>		<span class="n">self</span><span class="p">.</span><span class="n">userInteractionEnabled</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span></div><div class='line' id='LC28'><br/></div><div class='line' id='LC29'>		<span class="c1">// Default values for the progress bar</span></div><div class='line' id='LC30'>		<span class="n">self</span><span class="p">.</span><span class="n">MaxValue</span> <span class="o">=</span> <span class="mf">100.0f</span><span class="p">;</span></div><div class='line' id='LC31'>		<span class="n">self</span><span class="p">.</span><span class="n">Progress</span> <span class="o">=</span> <span class="mf">0.0f</span><span class="p">;</span></div><div class='line' id='LC32'><br/></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">self</span><span class="p">;</span></div><div class='line' id='LC35'><span class="p">}</span></div><div class='line' id='LC36'><br/></div><div class='line' id='LC37'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nf">setMaxValue:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">_MaxValue</span></div><div class='line' id='LC38'><span class="p">{</span></div><div class='line' id='LC39'>	<span class="n">MaxValue</span> <span class="o">=</span> <span class="n">_MaxValue</span><span class="p">;</span></div><div class='line' id='LC40'>	<span class="p">[</span><span class="n">self</span> <span class="n">setNeedsDisplay</span><span class="p">];</span></div><div class='line' id='LC41'><span class="p">}</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nl">setProgress:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">_Progress</span></div><div class='line' id='LC44'><span class="p">{</span></div><div class='line' id='LC45'>	<span class="n">Progress</span> <span class="o">=</span> <span class="n">_Progress</span><span class="p">;</span></div><div class='line' id='LC46'>	<span class="p">[</span><span class="n">self</span> <span class="n">setNeedsDisplay</span><span class="p">];</span></div><div class='line' id='LC47'><span class="p">}</span></div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nl">drawRect:</span><span class="p">(</span><span class="n">CGRect</span><span class="p">)</span><span class="n">rect</span></div><div class='line' id='LC50'><span class="p">{</span></div><div class='line' id='LC51'>	<span class="n">CGContextRef</span> <span class="n">context</span> <span class="o">=</span> <span class="n">UIGraphicsGetCurrentContext</span><span class="p">();</span> </div><div class='line' id='LC52'><br/></div><div class='line' id='LC53'>	<span class="c1">// Set the color of the circle to appear for the progressbar</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">CGContextSetRGBStrokeColor</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="mf">0.341</span><span class="p">,</span> <span class="mf">0.635</span><span class="p">,</span> <span class="mf">0.961</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">);</span> </div><div class='line' id='LC55'><br/></div><div class='line' id='LC56'>	<span class="c1">// Set the line width of the circle to appear</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">CGContextSetLineWidth</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="mf">10.0</span><span class="p">);</span> </div><div class='line' id='LC58'><br/></div><div class='line' id='LC59'>	<span class="c1">// Calculate the middle of the circle</span></div><div class='line' id='LC60'>	<span class="n">CGPoint</span> <span class="n">circleCenter</span> <span class="o">=</span> <span class="n">CGPointMake</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">self</span><span class="p">.</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">/</span> <span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC61'><br/></div><div class='line' id='LC62'>	<span class="c1">// Calculate the amount of degrees the circle needs to have filled</span></div><div class='line' id='LC63'>	<span class="kt">float</span> <span class="n">currentDegrees</span> <span class="o">=</span> <span class="p">(</span><span class="n">MAX_DEGREES</span> <span class="o">/</span> <span class="n">self</span><span class="p">.</span><span class="n">MaxValue</span><span class="p">)</span> <span class="o">*</span> <span class="n">self</span><span class="p">.</span><span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC64'><br/></div><div class='line' id='LC65'>	<span class="c1">// Draw the ARC (part of the circle</span></div><div class='line' id='LC66'>	<span class="n">CGContextAddArc</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">circleCenter</span><span class="p">.</span><span class="n">x</span> <span class="p">,</span> <span class="n">circleCenter</span><span class="p">.</span><span class="n">y</span><span class="p">,</span> <span class="n">CIRCLE_RADIUS</span><span class="p">,</span> <span class="n">radians</span><span class="p">(</span><span class="n">START_DEGREES</span><span class="p">),</span> <span class="n">radians</span><span class="p">(</span><span class="n">START_DEGREES</span> <span class="o">+</span> <span class="n">currentDegrees</span><span class="p">),</span> <span class="mi">0</span><span class="p">);</span> </div><div class='line' id='LC67'>	<span class="n">CGContextStrokePath</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC68'><span class="p">}</span></div><div class='line' id='LC69'><br/></div><div class='line' id='LC70'><br/></div><div class='line' id='LC71'><span class="k">@end</span></div><div class='line' id='LC72'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1378588/d61583a8c37ed11fac3905e5eb6adee8050c1e11/SBCircleProgressBar.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1378588#file_sb_circle_progress_bar.m" style="float:right;margin-right:10px;color:#666">SBCircleProgressBar.m</a>
            <a href="https://gist.github.com/1378588">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>You need to put this view OVER the image of your button. Because we do this, we also set userInteractionEnabled otherwise you wouldn&#8217;t be able to push your button anymore, because the progressbar would catch all the touches.</p>
<p>The method &#8220;radians&#8221; you see in there, is a small inline method used to calculate degrees to radians:
</p>
<pre class="syntax c">
static inline float radians(double degrees) { return degrees * M_PI / 180; }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/a-circle-progressbar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A circle progressbar</title>
		<link>http://www.sortedbits.com/a-circle-progressbar/</link>
		<comments>http://www.sortedbits.com/a-circle-progressbar/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 07:35:55 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1458</guid>
		<description><![CDATA[For a small application I wanted to write, I wanted to make a progressbar that is a circle instead of a normal horizontal bar. I wanted to press a button, which is just a normal image and then have a circle go around it, showing the progress. [...]]]></description>
			<content:encoded><![CDATA[<p>For a small application I wanted to write, I wanted to make a progressbar that is a circle instead of a normal horizontal bar. I wanted to press a button, which is just a normal image and then have a circle go around it, showing the progress.</p>
<p><a href="http://www.wim.me/wp-content/uploads/2011/11/SBCircleProgressBar.png" rel="wp-prettyPhoto[1458]"><img src="http://www.wim.me/wp-content/uploads/2011/11/SBCircleProgressBar.png" alt="" title="SBCircleProgressBar" width="218" height="184" class="alignnone size-full wp-image-1459" /></a><br />
Like you can see above in the image, it just looks awesome!</p>
<p>To achieve this, I followed a couple of simple steps:</p>
<ul>
<li>Create a cool button image, where you want the circle progressbar to appear in</li>
<li>Find out the center and radius of the circle progressbar, you can do this either by using an image editor or just guess some values and tweak them when they are not completely perfect.</li>
<li>Create a class subclassing a UIView, which has 2 properties : MaxValue (the value that is the maximum you want to use) and Progress (what is the current progress of the progressbar)</li>
<li>Put it all on another View and change the Progress property when the progress of your process changes</li>
</ul>
<div id="gist-1378588" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">//</span></div><div class='line' id='LC2'><span class="c1">//  SBCircleProgressBar.h</span></div><div class='line' id='LC3'><span class="c1">//</span></div><div class='line' id='LC4'><span class="c1">//  Created by Wim Haanstra on 17-11-11.</span></div><div class='line' id='LC5'><span class="c1">//  Copyright (c) 2011 Sorted Bits. All rights reserved.</span></div><div class='line' id='LC6'><span class="c1">//</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="k">@interface</span> <span class="nc">SBCircleProgressBar</span> : <span class="nc">UIView</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="c1">// The maximum value of the progressbar</span></div><div class='line' id='LC11'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">)</span> <span class="kt">float</span> <span class="n">MaxValue</span><span class="p">;</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="c1">// The current value of the progressbar</span></div><div class='line' id='LC14'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">)</span> <span class="kt">float</span> <span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="k">@end</span></div><div class='line' id='LC17'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1378588/02f81ee6ecdb257e2d68313ff611cfe1a21132d9/SBCircleProgressBar.h" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1378588#file_sb_circle_progress_bar.h" style="float:right;margin-right:10px;color:#666">SBCircleProgressBar.h</a>
            <a href="https://gist.github.com/1378588">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">//</span></div><div class='line' id='LC2'><span class="cp">//  SBCircleProgressBar.m</span></div><div class='line' id='LC3'><span class="cp">//</span></div><div class='line' id='LC4'><span class="cp">//  Created by Wim Haanstra on 17-11-11.</span></div><div class='line' id='LC5'><span class="cp">//  Copyright (c) 2011 Sorted Bits. All rights reserved.</span></div><div class='line' id='LC6'><span class="cp">//</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="cp">#import &quot;SBCircleProgressBar.h&quot;</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="k">@implementation</span> <span class="nc">SBCircleProgressBar</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'><span class="k">@synthesize</span> <span class="n">MaxValue</span><span class="p">,</span> <span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="cp">// SOME VALUES YOU CAN CHANGE TO CHANGE THE BEHAVIOUR OF THE PROGRESSBAR</span></div><div class='line' id='LC15'><span class="cp">#define MAX_DEGREES	360</span></div><div class='line' id='LC16'><span class="cp">#define START_DEGREES	270</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="cp">// Define the radius that the circle needs</span></div><div class='line' id='LC19'><span class="cp">#define CIRCLE_RADIUS	27</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'><span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">initWithFrame:</span><span class="p">(</span><span class="n">CGRect</span><span class="p">)</span><span class="nv">frame</span></div><div class='line' id='LC22'><span class="p">{</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">self</span> <span class="o">=</span> <span class="p">[</span><span class="n">super</span> <span class="nl">initWithFrame:</span><span class="n">frame</span><span class="p">];</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC25'><br/></div><div class='line' id='LC26'>		<span class="n">self</span><span class="p">.</span><span class="n">backgroundColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="n">clearColor</span><span class="p">];</span></div><div class='line' id='LC27'>		<span class="n">self</span><span class="p">.</span><span class="n">userInteractionEnabled</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span></div><div class='line' id='LC28'><br/></div><div class='line' id='LC29'>		<span class="c1">// Default values for the progress bar</span></div><div class='line' id='LC30'>		<span class="n">self</span><span class="p">.</span><span class="n">MaxValue</span> <span class="o">=</span> <span class="mf">100.0f</span><span class="p">;</span></div><div class='line' id='LC31'>		<span class="n">self</span><span class="p">.</span><span class="n">Progress</span> <span class="o">=</span> <span class="mf">0.0f</span><span class="p">;</span></div><div class='line' id='LC32'><br/></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">self</span><span class="p">;</span></div><div class='line' id='LC35'><span class="p">}</span></div><div class='line' id='LC36'><br/></div><div class='line' id='LC37'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nf">setMaxValue:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">_MaxValue</span></div><div class='line' id='LC38'><span class="p">{</span></div><div class='line' id='LC39'>	<span class="n">MaxValue</span> <span class="o">=</span> <span class="n">_MaxValue</span><span class="p">;</span></div><div class='line' id='LC40'>	<span class="p">[</span><span class="n">self</span> <span class="n">setNeedsDisplay</span><span class="p">];</span></div><div class='line' id='LC41'><span class="p">}</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nl">setProgress:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">_Progress</span></div><div class='line' id='LC44'><span class="p">{</span></div><div class='line' id='LC45'>	<span class="n">Progress</span> <span class="o">=</span> <span class="n">_Progress</span><span class="p">;</span></div><div class='line' id='LC46'>	<span class="p">[</span><span class="n">self</span> <span class="n">setNeedsDisplay</span><span class="p">];</span></div><div class='line' id='LC47'><span class="p">}</span></div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nl">drawRect:</span><span class="p">(</span><span class="n">CGRect</span><span class="p">)</span><span class="n">rect</span></div><div class='line' id='LC50'><span class="p">{</span></div><div class='line' id='LC51'>	<span class="n">CGContextRef</span> <span class="n">context</span> <span class="o">=</span> <span class="n">UIGraphicsGetCurrentContext</span><span class="p">();</span> </div><div class='line' id='LC52'><br/></div><div class='line' id='LC53'>	<span class="c1">// Set the color of the circle to appear for the progressbar</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">CGContextSetRGBStrokeColor</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="mf">0.341</span><span class="p">,</span> <span class="mf">0.635</span><span class="p">,</span> <span class="mf">0.961</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">);</span> </div><div class='line' id='LC55'><br/></div><div class='line' id='LC56'>	<span class="c1">// Set the line width of the circle to appear</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">CGContextSetLineWidth</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="mf">10.0</span><span class="p">);</span> </div><div class='line' id='LC58'><br/></div><div class='line' id='LC59'>	<span class="c1">// Calculate the middle of the circle</span></div><div class='line' id='LC60'>	<span class="n">CGPoint</span> <span class="n">circleCenter</span> <span class="o">=</span> <span class="n">CGPointMake</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">self</span><span class="p">.</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">/</span> <span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC61'><br/></div><div class='line' id='LC62'>	<span class="c1">// Calculate the amount of degrees the circle needs to have filled</span></div><div class='line' id='LC63'>	<span class="kt">float</span> <span class="n">currentDegrees</span> <span class="o">=</span> <span class="p">(</span><span class="n">MAX_DEGREES</span> <span class="o">/</span> <span class="n">self</span><span class="p">.</span><span class="n">MaxValue</span><span class="p">)</span> <span class="o">*</span> <span class="n">self</span><span class="p">.</span><span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC64'><br/></div><div class='line' id='LC65'>	<span class="c1">// Draw the ARC (part of the circle</span></div><div class='line' id='LC66'>	<span class="n">CGContextAddArc</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">circleCenter</span><span class="p">.</span><span class="n">x</span> <span class="p">,</span> <span class="n">circleCenter</span><span class="p">.</span><span class="n">y</span><span class="p">,</span> <span class="n">CIRCLE_RADIUS</span><span class="p">,</span> <span class="n">radians</span><span class="p">(</span><span class="n">START_DEGREES</span><span class="p">),</span> <span class="n">radians</span><span class="p">(</span><span class="n">START_DEGREES</span> <span class="o">+</span> <span class="n">currentDegrees</span><span class="p">),</span> <span class="mi">0</span><span class="p">);</span> </div><div class='line' id='LC67'>	<span class="n">CGContextStrokePath</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC68'><span class="p">}</span></div><div class='line' id='LC69'><br/></div><div class='line' id='LC70'><br/></div><div class='line' id='LC71'><span class="k">@end</span></div><div class='line' id='LC72'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1378588/d61583a8c37ed11fac3905e5eb6adee8050c1e11/SBCircleProgressBar.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1378588#file_sb_circle_progress_bar.m" style="float:right;margin-right:10px;color:#666">SBCircleProgressBar.m</a>
            <a href="https://gist.github.com/1378588">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>You need to put this view OVER the image of your button. Because we do this, we also set userInteractionEnabled otherwise you wouldn&#8217;t be able to push your button anymore, because the progressbar would catch all the touches.</p>
<p>The method &#8220;radians&#8221; you see in there, is a small inline method used to calculate degrees to radians:
</p>
<pre class="syntax c">
static inline float radians(double degrees) { return degrees * M_PI / 180; }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/a-circle-progressbar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>string extension methods for C#</title>
		<link>http://www.sortedbits.com/string-extension-methods-for-c/</link>
		<comments>http://www.sortedbits.com/string-extension-methods-for-c/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 15:07:46 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1386</guid>
		<description><![CDATA[I admit, I have a big passion for writing useful extension methods in C#. I love how easy they make my job and keep me from writing code over and over again. There are some methods I use regularly and most of them apply to the string [...]]]></description>
			<content:encoded><![CDATA[<p>I admit, I have a big passion for writing useful extension methods in C#. I love how easy they make my job and keep me from writing code over and over again.</p>
<p>There are some methods I use regularly and most of them apply to the string class. So without further ado, here are some of my favorites.</p>
<p><strong>Validating</strong></p>
<div id="gist-1302057" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">static</span> <span class="kt">bool</span> <span class="nf">IsInt</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">text</span><span class="p">)</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">integer</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">Int32</span><span class="p">.</span><span class="n">TryParse</span><span class="p">(</span><span class="n">text</span><span class="p">, autoload = </span> <span class="k">out</span> <span class="n">integer</span><span class="p">);</span></div><div class='line' id='LC5'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302057/adcdf50c97766dec972e2f43b3176acc82e8cf40/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302057#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1302057">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Of course you can create methods like this for all kind of types to support. This one just shows an example for the Int32 type. This way I can easily check if variables are correct. It is too bad that you can&#8217;t just parse the variable into the void so an extra variable is needed.</p>
<p>One other is an extension to make a valid filename from a string, like this:</p>
<div id="gist-1302069" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">static</span> <span class="k">public</span> <span class="kt">string</span> <span class="nf">ToValidFileName</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">name</span><span class="p">)</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">string</span> <span class="n">invalidChars</span> <span class="p">=</span> <span class="n">Regex</span><span class="p">.</span><span class="n">Escape</span><span class="p">(</span><span class="k">new</span> <span class="kt">string</span><span class="p">(</span><span class="n">Path</span><span class="p">.</span><span class="n">GetInvalidFileNameChars</span><span class="p">()));</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">string</span> <span class="n">invalidReStr</span> <span class="p">=</span> <span class="kt">string</span><span class="p">.</span><span class="n">Format</span><span class="p">(</span><span class="s">@&quot;[{0}]+&quot;</span><span class="p">,</span> <span class="n">invalidChars</span><span class="p">);</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">Regex</span><span class="p">.</span><span class="n">Replace</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">invalidReStr</span><span class="p">,</span> <span class="s">&quot;_&quot;</span><span class="p">);</span></div><div class='line' id='LC6'><span class="p">}</span> </div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302069/1b5ac77226be4d7acc7fcb8ff633592280a88e68/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302069#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1302069">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p><strong>Hashing</strong><br />
There are several reasons to hash a string, I personally use it to store passwords. Nowadays I use SHA1 hashing because MD5 is not that secure.</p>
<div id="gist-1302074" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">static</span> <span class="kt">string</span> <span class="nf">ToSHA1</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">text</span><span class="p">,</span> <span class="n">Encoding</span> <span class="n">enc</span><span class="p">)</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">byte</span><span class="p">[]</span> <span class="n">buffer</span> <span class="p">=</span> <span class="n">enc</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">text</span><span class="p">);</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">SHA1CryptoServiceProvider</span> <span class="n">cryptoTransformSHA1</span> <span class="p">=</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">SHA1CryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">string</span> <span class="n">hash</span> <span class="p">=</span> <span class="n">BitConverter</span><span class="p">.</span><span class="n">ToString</span><span class="p">(</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">cryptoTransformSHA1</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">buffer</span><span class="p">)).</span><span class="n">Replace</span><span class="p">(</span><span class="s">&quot;-&quot;</span><span class="p">,</span> <span class="s">&quot;&quot;</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">hash</span><span class="p">;</span></div><div class='line' id='LC9'><span class="p">}</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="k">public</span> <span class="k">static</span> <span class="kt">string</span> <span class="nf">ToMD5</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">input</span><span class="p">)</span></div><div class='line' id='LC12'><span class="p">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">MD5</span> <span class="n">md5</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">Security</span><span class="p">.</span><span class="n">Cryptography</span><span class="p">.</span><span class="n">MD5</span><span class="p">.</span><span class="n">Create</span><span class="p">();</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">byte</span><span class="p">[]</span> <span class="n">inputBytes</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">Encoding</span><span class="p">.</span><span class="n">ASCII</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">input</span><span class="p">);</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">byte</span><span class="p">[]</span> <span class="n">hash</span> <span class="p">=</span> <span class="n">md5</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">inputBytes</span><span class="p">);</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">StringBuilder</span> <span class="n">sb</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="p">();</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&amp;</span><span class="n">lt</span><span class="p">;</span> <span class="n">hash</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">sb</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="n">hash</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">ToString</span><span class="p">(&amp;</span><span class="n">quot</span><span class="p">;</span><span class="n">X2</span><span class="p">&amp;</span><span class="n">quot</span><span class="p">;));</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">sb</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span></div><div class='line' id='LC23'><span class="p">}</span> </div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302074/c584d2cf6e41c8193469fa44be887893f862afd0/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302074#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1302074">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p><strong>Encrypting/Decrypting</strong><br />
Encrypting and decrypting data is very important in one of my latest projects. For that reason I created these 2 extension methods. They encrypt and decrypt a string with a supplied passphrase. If there was no passphrase supplied, it will try to fetch one from the .config file.</p>
<div id="gist-1302080" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">static</span> <span class="kt">string</span> <span class="nf">Encrypt</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">Message</span><span class="p">,</span> <span class="kt">string</span> <span class="n">passphrase</span> <span class="p">=</span> <span class="s">&quot;&quot;</span><span class="p">)</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="k">if</span> <span class="p">(</span><span class="n">passphrase</span> <span class="p">==</span> <span class="s">&quot;&quot;</span><span class="p">)</span></div><div class='line' id='LC4'>		<span class="n">passphrase</span> <span class="p">=</span> <span class="n">ConfigurationManager</span><span class="p">.</span><span class="n">AppSettings</span><span class="p">[</span><span class="s">&quot;encrpytionPassphrase&quot;</span><span class="p">];</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">Results</span><span class="p">;</span></div><div class='line' id='LC7'>	<span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">UTF8Encoding</span> <span class="n">UTF8</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">UTF8Encoding</span><span class="p">();</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>	<span class="n">MD5CryptoServiceProvider</span> <span class="n">HashProvider</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MD5CryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC10'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">TDESKey</span> <span class="p">=</span> <span class="n">HashProvider</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">UTF8</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">passphrase</span><span class="p">));</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>	<span class="n">TripleDESCryptoServiceProvider</span> <span class="n">TDESAlgorithm</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TripleDESCryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Key</span> <span class="p">=</span> <span class="n">TDESKey</span><span class="p">;</span></div><div class='line' id='LC15'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Mode</span> <span class="p">=</span> <span class="n">CipherMode</span><span class="p">.</span><span class="n">ECB</span><span class="p">;</span></div><div class='line' id='LC16'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Padding</span> <span class="p">=</span> <span class="n">PaddingMode</span><span class="p">.</span><span class="n">PKCS7</span><span class="p">;</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">DataToEncrypt</span> <span class="p">=</span> <span class="n">UTF8</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">Message</span><span class="p">);</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'>	<span class="k">try</span></div><div class='line' id='LC21'>	<span class="p">{</span></div><div class='line' id='LC22'>		<span class="n">ICryptoTransform</span> <span class="n">Encryptor</span> <span class="p">=</span> <span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">CreateEncryptor</span><span class="p">();</span></div><div class='line' id='LC23'>		<span class="n">Results</span> <span class="p">=</span> <span class="n">Encryptor</span><span class="p">.</span><span class="n">TransformFinalBlock</span><span class="p">(</span><span class="n">DataToEncrypt</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="n">DataToEncrypt</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span></div><div class='line' id='LC24'>	<span class="p">}</span></div><div class='line' id='LC25'>	<span class="k">finally</span></div><div class='line' id='LC26'>	<span class="p">{</span></div><div class='line' id='LC27'>		<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Clear</span><span class="p">();</span></div><div class='line' id='LC28'>		<span class="n">HashProvider</span><span class="p">.</span><span class="n">Clear</span><span class="p">();</span></div><div class='line' id='LC29'>	<span class="p">}</span></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'>	<span class="k">return</span> <span class="n">Convert</span><span class="p">.</span><span class="n">ToBase64String</span><span class="p">(</span><span class="n">Results</span><span class="p">);</span></div><div class='line' id='LC32'><span class="p">}</span></div><div class='line' id='LC33'><br/></div><div class='line' id='LC34'><span class="k">public</span> <span class="k">static</span> <span class="kt">string</span> <span class="nf">Decrypt</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">Message</span><span class="p">,</span> <span class="kt">string</span> <span class="n">passphrase</span> <span class="p">=</span> <span class="s">&quot;&quot;</span><span class="p">)</span></div><div class='line' id='LC35'><span class="p">{</span></div><div class='line' id='LC36'>	<span class="k">if</span> <span class="p">(</span><span class="n">passphrase</span> <span class="p">==</span> <span class="s">&quot;&quot;</span><span class="p">)</span></div><div class='line' id='LC37'>		<span class="n">passphrase</span> <span class="p">=</span> <span class="n">ConfigurationManager</span><span class="p">.</span><span class="n">AppSettings</span><span class="p">[</span><span class="s">&quot;encrpytionPassphrase&quot;</span><span class="p">];</span></div><div class='line' id='LC38'>	<span class="k">if</span> <span class="p">(</span><span class="n">Message</span> <span class="p">==</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">)</span></div><div class='line' id='LC39'>		<span class="k">return</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">;</span></div><div class='line' id='LC40'><br/></div><div class='line' id='LC41'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">Results</span><span class="p">;</span></div><div class='line' id='LC42'>	<span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">UTF8Encoding</span> <span class="n">UTF8</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">UTF8Encoding</span><span class="p">();</span></div><div class='line' id='LC43'><br/></div><div class='line' id='LC44'>	<span class="n">MD5CryptoServiceProvider</span> <span class="n">HashProvider</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MD5CryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC45'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">TDESKey</span> <span class="p">=</span> <span class="n">HashProvider</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">UTF8</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">passphrase</span><span class="p">));</span></div><div class='line' id='LC46'><br/></div><div class='line' id='LC47'>	<span class="n">TripleDESCryptoServiceProvider</span> <span class="n">TDESAlgorithm</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TripleDESCryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Key</span> <span class="p">=</span> <span class="n">TDESKey</span><span class="p">;</span></div><div class='line' id='LC50'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Mode</span> <span class="p">=</span> <span class="n">CipherMode</span><span class="p">.</span><span class="n">ECB</span><span class="p">;</span></div><div class='line' id='LC51'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Padding</span> <span class="p">=</span> <span class="n">PaddingMode</span><span class="p">.</span><span class="n">PKCS7</span><span class="p">;</span></div><div class='line' id='LC52'><br/></div><div class='line' id='LC53'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">DataToDecrypt</span> <span class="p">=</span> <span class="n">Convert</span><span class="p">.</span><span class="n">FromBase64String</span><span class="p">(</span><span class="n">Message</span><span class="p">);</span></div><div class='line' id='LC54'><br/></div><div class='line' id='LC55'>	<span class="k">try</span></div><div class='line' id='LC56'>	<span class="p">{</span></div><div class='line' id='LC57'>		<span class="n">ICryptoTransform</span> <span class="n">Decryptor</span> <span class="p">=</span> <span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">CreateDecryptor</span><span class="p">();</span></div><div class='line' id='LC58'>		<span class="n">Results</span> <span class="p">=</span> <span class="n">Decryptor</span><span class="p">.</span><span class="n">TransformFinalBlock</span><span class="p">(</span><span class="n">DataToDecrypt</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="n">DataToDecrypt</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span></div><div class='line' id='LC59'>	<span class="p">}</span></div><div class='line' id='LC60'>	<span class="k">catch</span></div><div class='line' id='LC61'>	<span class="p">{</span></div><div class='line' id='LC62'>		<span class="n">Results</span> <span class="p">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="p">[</span><span class="m">0</span><span class="p">];</span></div><div class='line' id='LC63'>	<span class="p">}</span></div><div class='line' id='LC64'>	<span class="k">finally</span></div><div class='line' id='LC65'>	<span class="p">{</span></div><div class='line' id='LC66'>		<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Clear</span><span class="p">();</span></div><div class='line' id='LC67'>		<span class="n">HashProvider</span><span class="p">.</span><span class="n">Clear</span><span class="p">();</span></div><div class='line' id='LC68'>	<span class="p">}</span></div><div class='line' id='LC69'><br/></div><div class='line' id='LC70'>	<span class="k">return</span> <span class="n">UTF8</span><span class="p">.</span><span class="n">GetString</span><span class="p">(</span><span class="n">Results</span><span class="p">);</span></div><div class='line' id='LC71'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302080/d9b4a9b28870fac5d7b116b901b307eee68a1f68/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302080#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1302080">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/string-extension-methods-for-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>string extension methods for C#</title>
		<link>http://www.sortedbits.com/string-extension-methods-for-c/</link>
		<comments>http://www.sortedbits.com/string-extension-methods-for-c/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 15:07:46 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1386</guid>
		<description><![CDATA[I admit, I have a big passion for writing useful extension methods in C#. I love how easy they make my job and keep me from writing code over and over again. There are some methods I use regularly and most of them apply to the string [...]]]></description>
			<content:encoded><![CDATA[<p>I admit, I have a big passion for writing useful extension methods in C#. I love how easy they make my job and keep me from writing code over and over again.</p>
<p>There are some methods I use regularly and most of them apply to the string class. So without further ado, here are some of my favorites.</p>
<p><strong>Validating</strong></p>
<div id="gist-1302057" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">static</span> <span class="kt">bool</span> <span class="nf">IsInt</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">text</span><span class="p">)</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">integer</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">Int32</span><span class="p">.</span><span class="n">TryParse</span><span class="p">(</span><span class="n">text</span><span class="p">, autoload = </span> <span class="k">out</span> <span class="n">integer</span><span class="p">);</span></div><div class='line' id='LC5'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302057/adcdf50c97766dec972e2f43b3176acc82e8cf40/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302057#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1302057">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Of course you can create methods like this for all kind of types to support. This one just shows an example for the Int32 type. This way I can easily check if variables are correct. It is too bad that you can&#8217;t just parse the variable into the void so an extra variable is needed.</p>
<p>One other is an extension to make a valid filename from a string, like this:</p>
<div id="gist-1302069" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">static</span> <span class="k">public</span> <span class="kt">string</span> <span class="nf">ToValidFileName</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">name</span><span class="p">)</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">string</span> <span class="n">invalidChars</span> <span class="p">=</span> <span class="n">Regex</span><span class="p">.</span><span class="n">Escape</span><span class="p">(</span><span class="k">new</span> <span class="kt">string</span><span class="p">(</span><span class="n">Path</span><span class="p">.</span><span class="n">GetInvalidFileNameChars</span><span class="p">()));</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">string</span> <span class="n">invalidReStr</span> <span class="p">=</span> <span class="kt">string</span><span class="p">.</span><span class="n">Format</span><span class="p">(</span><span class="s">@&quot;[{0}]+&quot;</span><span class="p">,</span> <span class="n">invalidChars</span><span class="p">);</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">Regex</span><span class="p">.</span><span class="n">Replace</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">invalidReStr</span><span class="p">,</span> <span class="s">&quot;_&quot;</span><span class="p">);</span></div><div class='line' id='LC6'><span class="p">}</span> </div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302069/1b5ac77226be4d7acc7fcb8ff633592280a88e68/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302069#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1302069">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p><strong>Hashing</strong><br />
There are several reasons to hash a string, I personally use it to store passwords. Nowadays I use SHA1 hashing because MD5 is not that secure.</p>
<div id="gist-1302074" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">static</span> <span class="kt">string</span> <span class="nf">ToSHA1</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">text</span><span class="p">,</span> <span class="n">Encoding</span> <span class="n">enc</span><span class="p">)</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">byte</span><span class="p">[]</span> <span class="n">buffer</span> <span class="p">=</span> <span class="n">enc</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">text</span><span class="p">);</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">SHA1CryptoServiceProvider</span> <span class="n">cryptoTransformSHA1</span> <span class="p">=</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">SHA1CryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">string</span> <span class="n">hash</span> <span class="p">=</span> <span class="n">BitConverter</span><span class="p">.</span><span class="n">ToString</span><span class="p">(</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">cryptoTransformSHA1</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">buffer</span><span class="p">)).</span><span class="n">Replace</span><span class="p">(</span><span class="s">&quot;-&quot;</span><span class="p">,</span> <span class="s">&quot;&quot;</span><span class="p">);</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">hash</span><span class="p">;</span></div><div class='line' id='LC9'><span class="p">}</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="k">public</span> <span class="k">static</span> <span class="kt">string</span> <span class="nf">ToMD5</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">input</span><span class="p">)</span></div><div class='line' id='LC12'><span class="p">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">MD5</span> <span class="n">md5</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">Security</span><span class="p">.</span><span class="n">Cryptography</span><span class="p">.</span><span class="n">MD5</span><span class="p">.</span><span class="n">Create</span><span class="p">();</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">byte</span><span class="p">[]</span> <span class="n">inputBytes</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">Encoding</span><span class="p">.</span><span class="n">ASCII</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">input</span><span class="p">);</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">byte</span><span class="p">[]</span> <span class="n">hash</span> <span class="p">=</span> <span class="n">md5</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">inputBytes</span><span class="p">);</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">StringBuilder</span> <span class="n">sb</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="p">();</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&amp;</span><span class="n">lt</span><span class="p">;</span> <span class="n">hash</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">sb</span><span class="p">.</span><span class="n">Append</span><span class="p">(</span><span class="n">hash</span><span class="p">[</span><span class="n">i</span><span class="p">].</span><span class="n">ToString</span><span class="p">(&amp;</span><span class="n">quot</span><span class="p">;</span><span class="n">X2</span><span class="p">&amp;</span><span class="n">quot</span><span class="p">;));</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">sb</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span></div><div class='line' id='LC23'><span class="p">}</span> </div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302074/c584d2cf6e41c8193469fa44be887893f862afd0/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302074#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1302074">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p><strong>Encrypting/Decrypting</strong><br />
Encrypting and decrypting data is very important in one of my latest projects. For that reason I created these 2 extension methods. They encrypt and decrypt a string with a supplied passphrase. If there was no passphrase supplied, it will try to fetch one from the .config file.</p>
<div id="gist-1302080" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">static</span> <span class="kt">string</span> <span class="nf">Encrypt</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">Message</span><span class="p">,</span> <span class="kt">string</span> <span class="n">passphrase</span> <span class="p">=</span> <span class="s">&quot;&quot;</span><span class="p">)</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="k">if</span> <span class="p">(</span><span class="n">passphrase</span> <span class="p">==</span> <span class="s">&quot;&quot;</span><span class="p">)</span></div><div class='line' id='LC4'>		<span class="n">passphrase</span> <span class="p">=</span> <span class="n">ConfigurationManager</span><span class="p">.</span><span class="n">AppSettings</span><span class="p">[</span><span class="s">&quot;encrpytionPassphrase&quot;</span><span class="p">];</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">Results</span><span class="p">;</span></div><div class='line' id='LC7'>	<span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">UTF8Encoding</span> <span class="n">UTF8</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">UTF8Encoding</span><span class="p">();</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>	<span class="n">MD5CryptoServiceProvider</span> <span class="n">HashProvider</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MD5CryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC10'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">TDESKey</span> <span class="p">=</span> <span class="n">HashProvider</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">UTF8</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">passphrase</span><span class="p">));</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>	<span class="n">TripleDESCryptoServiceProvider</span> <span class="n">TDESAlgorithm</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TripleDESCryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Key</span> <span class="p">=</span> <span class="n">TDESKey</span><span class="p">;</span></div><div class='line' id='LC15'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Mode</span> <span class="p">=</span> <span class="n">CipherMode</span><span class="p">.</span><span class="n">ECB</span><span class="p">;</span></div><div class='line' id='LC16'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Padding</span> <span class="p">=</span> <span class="n">PaddingMode</span><span class="p">.</span><span class="n">PKCS7</span><span class="p">;</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">DataToEncrypt</span> <span class="p">=</span> <span class="n">UTF8</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">Message</span><span class="p">);</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'>	<span class="k">try</span></div><div class='line' id='LC21'>	<span class="p">{</span></div><div class='line' id='LC22'>		<span class="n">ICryptoTransform</span> <span class="n">Encryptor</span> <span class="p">=</span> <span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">CreateEncryptor</span><span class="p">();</span></div><div class='line' id='LC23'>		<span class="n">Results</span> <span class="p">=</span> <span class="n">Encryptor</span><span class="p">.</span><span class="n">TransformFinalBlock</span><span class="p">(</span><span class="n">DataToEncrypt</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="n">DataToEncrypt</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span></div><div class='line' id='LC24'>	<span class="p">}</span></div><div class='line' id='LC25'>	<span class="k">finally</span></div><div class='line' id='LC26'>	<span class="p">{</span></div><div class='line' id='LC27'>		<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Clear</span><span class="p">();</span></div><div class='line' id='LC28'>		<span class="n">HashProvider</span><span class="p">.</span><span class="n">Clear</span><span class="p">();</span></div><div class='line' id='LC29'>	<span class="p">}</span></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'>	<span class="k">return</span> <span class="n">Convert</span><span class="p">.</span><span class="n">ToBase64String</span><span class="p">(</span><span class="n">Results</span><span class="p">);</span></div><div class='line' id='LC32'><span class="p">}</span></div><div class='line' id='LC33'><br/></div><div class='line' id='LC34'><span class="k">public</span> <span class="k">static</span> <span class="kt">string</span> <span class="nf">Decrypt</span><span class="p">(</span><span class="k">this</span> <span class="kt">string</span> <span class="n">Message</span><span class="p">,</span> <span class="kt">string</span> <span class="n">passphrase</span> <span class="p">=</span> <span class="s">&quot;&quot;</span><span class="p">)</span></div><div class='line' id='LC35'><span class="p">{</span></div><div class='line' id='LC36'>	<span class="k">if</span> <span class="p">(</span><span class="n">passphrase</span> <span class="p">==</span> <span class="s">&quot;&quot;</span><span class="p">)</span></div><div class='line' id='LC37'>		<span class="n">passphrase</span> <span class="p">=</span> <span class="n">ConfigurationManager</span><span class="p">.</span><span class="n">AppSettings</span><span class="p">[</span><span class="s">&quot;encrpytionPassphrase&quot;</span><span class="p">];</span></div><div class='line' id='LC38'>	<span class="k">if</span> <span class="p">(</span><span class="n">Message</span> <span class="p">==</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">)</span></div><div class='line' id='LC39'>		<span class="k">return</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">;</span></div><div class='line' id='LC40'><br/></div><div class='line' id='LC41'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">Results</span><span class="p">;</span></div><div class='line' id='LC42'>	<span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">UTF8Encoding</span> <span class="n">UTF8</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Text</span><span class="p">.</span><span class="n">UTF8Encoding</span><span class="p">();</span></div><div class='line' id='LC43'><br/></div><div class='line' id='LC44'>	<span class="n">MD5CryptoServiceProvider</span> <span class="n">HashProvider</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MD5CryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC45'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">TDESKey</span> <span class="p">=</span> <span class="n">HashProvider</span><span class="p">.</span><span class="n">ComputeHash</span><span class="p">(</span><span class="n">UTF8</span><span class="p">.</span><span class="n">GetBytes</span><span class="p">(</span><span class="n">passphrase</span><span class="p">));</span></div><div class='line' id='LC46'><br/></div><div class='line' id='LC47'>	<span class="n">TripleDESCryptoServiceProvider</span> <span class="n">TDESAlgorithm</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TripleDESCryptoServiceProvider</span><span class="p">();</span></div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Key</span> <span class="p">=</span> <span class="n">TDESKey</span><span class="p">;</span></div><div class='line' id='LC50'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Mode</span> <span class="p">=</span> <span class="n">CipherMode</span><span class="p">.</span><span class="n">ECB</span><span class="p">;</span></div><div class='line' id='LC51'>	<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Padding</span> <span class="p">=</span> <span class="n">PaddingMode</span><span class="p">.</span><span class="n">PKCS7</span><span class="p">;</span></div><div class='line' id='LC52'><br/></div><div class='line' id='LC53'>	<span class="kt">byte</span><span class="p">[]</span> <span class="n">DataToDecrypt</span> <span class="p">=</span> <span class="n">Convert</span><span class="p">.</span><span class="n">FromBase64String</span><span class="p">(</span><span class="n">Message</span><span class="p">);</span></div><div class='line' id='LC54'><br/></div><div class='line' id='LC55'>	<span class="k">try</span></div><div class='line' id='LC56'>	<span class="p">{</span></div><div class='line' id='LC57'>		<span class="n">ICryptoTransform</span> <span class="n">Decryptor</span> <span class="p">=</span> <span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">CreateDecryptor</span><span class="p">();</span></div><div class='line' id='LC58'>		<span class="n">Results</span> <span class="p">=</span> <span class="n">Decryptor</span><span class="p">.</span><span class="n">TransformFinalBlock</span><span class="p">(</span><span class="n">DataToDecrypt</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="n">DataToDecrypt</span><span class="p">.</span><span class="n">Length</span><span class="p">);</span></div><div class='line' id='LC59'>	<span class="p">}</span></div><div class='line' id='LC60'>	<span class="k">catch</span></div><div class='line' id='LC61'>	<span class="p">{</span></div><div class='line' id='LC62'>		<span class="n">Results</span> <span class="p">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="p">[</span><span class="m">0</span><span class="p">];</span></div><div class='line' id='LC63'>	<span class="p">}</span></div><div class='line' id='LC64'>	<span class="k">finally</span></div><div class='line' id='LC65'>	<span class="p">{</span></div><div class='line' id='LC66'>		<span class="n">TDESAlgorithm</span><span class="p">.</span><span class="n">Clear</span><span class="p">();</span></div><div class='line' id='LC67'>		<span class="n">HashProvider</span><span class="p">.</span><span class="n">Clear</span><span class="p">();</span></div><div class='line' id='LC68'>	<span class="p">}</span></div><div class='line' id='LC69'><br/></div><div class='line' id='LC70'>	<span class="k">return</span> <span class="n">UTF8</span><span class="p">.</span><span class="n">GetString</span><span class="p">(</span><span class="n">Results</span><span class="p">);</span></div><div class='line' id='LC71'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302080/d9b4a9b28870fac5d7b116b901b307eee68a1f68/gistfile1.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302080#file_gistfile1.cs" style="float:right;margin-right:10px;color:#666">gistfile1.cs</a>
            <a href="https://gist.github.com/1302080">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/string-extension-methods-for-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the interface orientations from your project definition</title>
		<link>http://www.sortedbits.com/interfaceorientations/</link>
		<comments>http://www.sortedbits.com/interfaceorientations/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 06:25:35 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1379</guid>
		<description><![CDATA[I have always been annoyed by the way you have to handle your interface orientation detection and which orientations you allow for your application. You can specify the orientations in your project&#8217;s plist file, but I am not completely sure why the UIViewControllers don&#8217;t use this by [...]]]></description>
			<content:encoded><![CDATA[<p>I have always been annoyed by the way you have to handle your interface orientation detection and which orientations you allow for your application. You can specify the orientations in your project&#8217;s plist file, but I am not completely sure why the UIViewControllers don&#8217;t use this by default.</p>
<p>So for that purpose I made a method a long time ago, which I use on most UIViewControllers, except when they allow different interface orientations then default. I use this method as a static on some class I have, which helps me do pretty default stuff.</p>
<div id="gist-1302088" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">+</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span> <span class="nf">shouldAllowRotateToInterfaceOrientation:</span><span class="p">(</span><span class="n">UIInterfaceOrientation</span><span class="p">)</span> <span class="n">interfaceOrientation</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="n">NSString</span><span class="o">*</span> <span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;&quot;</span><span class="p">;</span></div><div class='line' id='LC4'>	<span class="k">switch</span> <span class="p">(</span><span class="n">interfaceOrientation</span><span class="p">)</span></div><div class='line' id='LC5'>	<span class="p">{</span></div><div class='line' id='LC6'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationLandscapeLeft:</span></div><div class='line' id='LC7'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationLandscapeLeft&quot;</span><span class="p">;</span></div><div class='line' id='LC8'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC9'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationLandscapeRight:</span></div><div class='line' id='LC10'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationLandscapeRight&quot;</span><span class="p">;</span></div><div class='line' id='LC11'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC12'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationPortrait:</span></div><div class='line' id='LC13'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationPortrait&quot;</span><span class="p">;</span></div><div class='line' id='LC14'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC15'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationPortraitUpsideDown:</span></div><div class='line' id='LC16'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationPortraitUpsideDown&quot;</span><span class="p">;</span></div><div class='line' id='LC17'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC18'>	<span class="p">}</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'>	<span class="n">NSString</span><span class="o">*</span> <span class="n">supportedOrientationsKey</span> <span class="o">=</span> <span class="s">@&quot;UISupportedInterfaceOrientations&quot;</span><span class="p">;</span></div><div class='line' id='LC21'>	<span class="k">if</span><span class="p">([</span><span class="n">UIDevice</span> <span class="n">currentDevice</span><span class="p">].</span><span class="n">userInterfaceIdiom</span> <span class="o">==</span> <span class="n">UIUserInterfaceIdiomPad</span><span class="p">)</span></div><div class='line' id='LC22'>		<span class="n">supportedOrientationsKey</span> <span class="o">=</span> <span class="s">@&quot;UISupportedInterfaceOrientations~ipad&quot;</span><span class="p">;</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'>	<span class="n">NSArray</span><span class="o">*</span> <span class="n">allowedOrientations</span> <span class="o">=</span> <span class="p">[[[</span><span class="n">NSBundle</span> <span class="n">mainBundle</span><span class="p">]</span> <span class="n">infoDictionary</span><span class="p">]</span> <span class="nl">objectForKey:</span><span class="n">supportedOrientationsKey</span><span class="p">];</span></div><div class='line' id='LC25'>	<span class="k">for</span> <span class="p">(</span><span class="n">NSString</span><span class="o">*</span> <span class="n">allowedOrientation</span> <span class="k">in</span> <span class="n">allowedOrientations</span><span class="p">)</span></div><div class='line' id='LC26'>	<span class="p">{</span></div><div class='line' id='LC27'>		<span class="k">if</span> <span class="p">([</span><span class="n">allowedOrientation</span> <span class="nl">isEqualToString:</span><span class="n">currentOrientation</span><span class="p">])</span></div><div class='line' id='LC28'>			<span class="k">return</span> <span class="n">YES</span><span class="p">;</span></div><div class='line' id='LC29'>	<span class="p">}</span></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'>	<span class="k">return</span> <span class="n">NO</span><span class="p">;</span></div><div class='line' id='LC32'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302088/6e24eb511bcfab7ef6636aa8856d6e00180ed666/gistfile1.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302088#file_gistfile1.m" style="float:right;margin-right:10px;color:#666">gistfile1.m</a>
            <a href="https://gist.github.com/1302088">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And you can just use it like this:</p>
<div id="gist-1302091" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">shouldAutorotateToInterfaceOrientation:</span><span class="p">(</span><span class="n">UIInterfaceOrientation</span><span class="p">)</span><span class="nv">interfaceOrientation</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="k">return</span> <span class="p">[</span><span class="n">WHUtilities</span> <span class="nl">shouldAllowRotateToInterfaceOrientation:</span><span class="n">interfaceOrientation</span><span class="p">];</span></div><div class='line' id='LC4'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302091/61be6b27c4da8765646ea41f812308ae2854e425/gistfile1.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302091#file_gistfile1.m" style="float:right;margin-right:10px;color:#666">gistfile1.m</a>
            <a href="https://gist.github.com/1302091">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/interfaceorientations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the interface orientations from your project definition</title>
		<link>http://www.sortedbits.com/interfaceorientations/</link>
		<comments>http://www.sortedbits.com/interfaceorientations/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 06:25:35 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1379</guid>
		<description><![CDATA[I have always been annoyed by the way you have to handle your interface orientation detection and which orientations you allow for your application. You can specify the orientations in your project&#8217;s plist file, but I am not completely sure why the UIViewControllers don&#8217;t use this by [...]]]></description>
			<content:encoded><![CDATA[<p>I have always been annoyed by the way you have to handle your interface orientation detection and which orientations you allow for your application. You can specify the orientations in your project&#8217;s plist file, but I am not completely sure why the UIViewControllers don&#8217;t use this by default.</p>
<p>So for that purpose I made a method a long time ago, which I use on most UIViewControllers, except when they allow different interface orientations then default. I use this method as a static on some class I have, which helps me do pretty default stuff.</p>
<div id="gist-1302088" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">+</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span> <span class="nf">shouldAllowRotateToInterfaceOrientation:</span><span class="p">(</span><span class="n">UIInterfaceOrientation</span><span class="p">)</span> <span class="n">interfaceOrientation</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="n">NSString</span><span class="o">*</span> <span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;&quot;</span><span class="p">;</span></div><div class='line' id='LC4'>	<span class="k">switch</span> <span class="p">(</span><span class="n">interfaceOrientation</span><span class="p">)</span></div><div class='line' id='LC5'>	<span class="p">{</span></div><div class='line' id='LC6'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationLandscapeLeft:</span></div><div class='line' id='LC7'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationLandscapeLeft&quot;</span><span class="p">;</span></div><div class='line' id='LC8'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC9'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationLandscapeRight:</span></div><div class='line' id='LC10'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationLandscapeRight&quot;</span><span class="p">;</span></div><div class='line' id='LC11'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC12'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationPortrait:</span></div><div class='line' id='LC13'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationPortrait&quot;</span><span class="p">;</span></div><div class='line' id='LC14'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC15'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationPortraitUpsideDown:</span></div><div class='line' id='LC16'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationPortraitUpsideDown&quot;</span><span class="p">;</span></div><div class='line' id='LC17'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC18'>	<span class="p">}</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'>	<span class="n">NSString</span><span class="o">*</span> <span class="n">supportedOrientationsKey</span> <span class="o">=</span> <span class="s">@&quot;UISupportedInterfaceOrientations&quot;</span><span class="p">;</span></div><div class='line' id='LC21'>	<span class="k">if</span><span class="p">([</span><span class="n">UIDevice</span> <span class="n">currentDevice</span><span class="p">].</span><span class="n">userInterfaceIdiom</span> <span class="o">==</span> <span class="n">UIUserInterfaceIdiomPad</span><span class="p">)</span></div><div class='line' id='LC22'>		<span class="n">supportedOrientationsKey</span> <span class="o">=</span> <span class="s">@&quot;UISupportedInterfaceOrientations~ipad&quot;</span><span class="p">;</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'>	<span class="n">NSArray</span><span class="o">*</span> <span class="n">allowedOrientations</span> <span class="o">=</span> <span class="p">[[[</span><span class="n">NSBundle</span> <span class="n">mainBundle</span><span class="p">]</span> <span class="n">infoDictionary</span><span class="p">]</span> <span class="nl">objectForKey:</span><span class="n">supportedOrientationsKey</span><span class="p">];</span></div><div class='line' id='LC25'>	<span class="k">for</span> <span class="p">(</span><span class="n">NSString</span><span class="o">*</span> <span class="n">allowedOrientation</span> <span class="k">in</span> <span class="n">allowedOrientations</span><span class="p">)</span></div><div class='line' id='LC26'>	<span class="p">{</span></div><div class='line' id='LC27'>		<span class="k">if</span> <span class="p">([</span><span class="n">allowedOrientation</span> <span class="nl">isEqualToString:</span><span class="n">currentOrientation</span><span class="p">])</span></div><div class='line' id='LC28'>			<span class="k">return</span> <span class="n">YES</span><span class="p">;</span></div><div class='line' id='LC29'>	<span class="p">}</span></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'>	<span class="k">return</span> <span class="n">NO</span><span class="p">;</span></div><div class='line' id='LC32'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302088/6e24eb511bcfab7ef6636aa8856d6e00180ed666/gistfile1.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302088#file_gistfile1.m" style="float:right;margin-right:10px;color:#666">gistfile1.m</a>
            <a href="https://gist.github.com/1302088">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And you can just use it like this:</p>
<div id="gist-1302091" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">shouldAutorotateToInterfaceOrientation:</span><span class="p">(</span><span class="n">UIInterfaceOrientation</span><span class="p">)</span><span class="nv">interfaceOrientation</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="k">return</span> <span class="p">[</span><span class="n">WHUtilities</span> <span class="nl">shouldAllowRotateToInterfaceOrientation:</span><span class="n">interfaceOrientation</span><span class="p">];</span></div><div class='line' id='LC4'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302091/61be6b27c4da8765646ea41f812308ae2854e425/gistfile1.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302091#file_gistfile1.m" style="float:right;margin-right:10px;color:#666">gistfile1.m</a>
            <a href="https://gist.github.com/1302091">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/interfaceorientations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protected: Localize your UIViews (the easy way)</title>
		<link>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/</link>
		<comments>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 13:21:35 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[cocoa-touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1357</guid>
		<description><![CDATA[There is no excerpt because this is a protected post.]]></description>
			<content:encoded><![CDATA[<form action="http://www.sortedbits.com/wp-pass.php" method="post">
<p>This post is password protected. To view it please enter your password below:</p>
<p><label for="pwbox-1357">Password:<br />
<input name="post_password" id="pwbox-1357" type="password" size="20" /></label><br />
<input type="submit" name="Submit" value="Submit" /></p></form>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protected: Localize your UIViews (the easy way)</title>
		<link>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/</link>
		<comments>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 13:21:35 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[cocoa-touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1357</guid>
		<description><![CDATA[There is no excerpt because this is a protected post.]]></description>
			<content:encoded><![CDATA[<form action="http://www.sortedbits.com/wp-pass.php" method="post">
<p>This post is password protected. To view it please enter your password below:</p>
<p><label for="pwbox-1357">Password:<br />
<input name="post_password" id="pwbox-1357" type="password" size="20" /></label><br />
<input type="submit" name="Submit" value="Submit" /></p></form>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

