<?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>Jeff Kelley’s Blog &#187; Objective-C</title>
	<atom:link href="http://blog.slaunchaman.com/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.slaunchaman.com</link>
	<description>Mac tips, iPhone applications, and the like</description>
	<lastBuildDate>Thu, 19 Aug 2010 21:14:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Quick Tip: Don&#8217;t Do This</title>
		<link>http://blog.slaunchaman.com/2010/05/10/quick-tip-dont-do-this/</link>
		<comments>http://blog.slaunchaman.com/2010/05/10/quick-tip-dont-do-this/#comments</comments>
		<pubDate>Tue, 11 May 2010 04:01:44 +0000</pubDate>
		<dc:creator>Jeff Kelley</dc:creator>
				<category><![CDATA[Programming Tips]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[gotchas]]></category>
		<category><![CDATA[NSObject]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Objective-C Runtime]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.slaunchaman.com/?p=263</guid>
		<description><![CDATA[I could not find out]]></description>
			<content:encoded><![CDATA[<p>I could <em>not</em> find out where a bug was coming from for the life of me today. Naturally it one of those &ldquo;assignment instead of equality&rdquo; bugs that seem to crop up when trying to code too quickly. The difference here was that I had implemented a subclass of <code>NSObject</code> that reimplemented the method <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/doc/uid/20000050-SW5"><code>+resolveInstanceMethod:</code></a>. So, the code went like this:<code>
<pre>+ ( BOOL )resolveInstanceMethod:( SEL )sel
{
    if ( sel = @selector( setFoobar: )) {
        return class_addMethod([ self class ], sel, ( IMP )setFB, "v@:@" );
    } else if ( sel = @selector( foobar )) {
        return class_addMethod([ self class ], sel, ( IMP )getFB, "@@:" );
    } else {
        return [ super resolveInstanceMethod:sel ];
    }
}</pre>
<p></code><br />
For those of you keeping score at home, when the Objective-C runtime tried to resolve <em>any</em> selector, this method happily added a selector for <code>-setFoobar:</code> to <code>self</code>&rsquo;s class and returned <code>YES</code>.</p>
<p>Don&rsquo;t do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.slaunchaman.com/2010/05/10/quick-tip-dont-do-this/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New iPhone Project: uWarranty</title>
		<link>http://blog.slaunchaman.com/2010/03/09/new-iphone-project-uwarranty/</link>
		<comments>http://blog.slaunchaman.com/2010/03/09/new-iphone-project-uwarranty/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 17:56:59 +0000</pubDate>
		<dc:creator>Jeff Kelley</dc:creator>
				<category><![CDATA[My Software]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[uWarranty]]></category>

		<guid isPermaLink="false">http://blog.slaunchaman.com/?p=244</guid>
		<description><![CDATA[So, I created a new]]></description>
			<content:encoded><![CDATA[<p>So, I created a new iPhone application called uWarranty. It used an unpublished Apple API for warranty status (from selfsolve.apple.com), and so I got this after submitting it:</p>
<blockquote><p>Thank you for submitting your application to the App Store. Unfortunately, your application, uWarranty, cannot be added to the App Store because it violates section 3.3.7 of the iPhone Developer Program License Agreement:</p>
<p>&#8220;Applications may not perform any functions or link to any content or use any robot, spider, site search or other retrieval application or device to scrape, retrieve or index services provided by Apple or its licensors, or to collect, disseminate or use information about users for any unauthorized purpose.&#8221;</p></blockquote>
<p>OK, I get it. That&rsquo;s fine and is Apple&rsquo;s prerogative. But now I have this program and all the development time that went into it. So what do I do? I guess the answer is to open-source it, just like <a title="AppSales Mobile" href="http://github.com/omz/AppSales-Mobile" target="_blank">AppSales Mobile</a>. Watch this space for more details as I clean up the code and throw it up on a public repository somewhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.slaunchaman.com/2010/03/09/new-iphone-project-uwarranty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cocoa Touch Tutorial: Stripping Non-Alphanumeric Characters on Entry in a UITextField</title>
		<link>http://blog.slaunchaman.com/2010/02/26/cocoa-touch-tutorial-stripping-non-alphanumeric-characters-on-entry-in-a-uitextfield/</link>
		<comments>http://blog.slaunchaman.com/2010/02/26/cocoa-touch-tutorial-stripping-non-alphanumeric-characters-on-entry-in-a-uitextfield/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 06:14:15 +0000</pubDate>
		<dc:creator>Jeff Kelley</dc:creator>
				<category><![CDATA[Programming Tips]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSCharacterSet]]></category>
		<category><![CDATA[NSMutableString]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[UITextField]]></category>
		<category><![CDATA[UITextFieldDelegate]]></category>

		<guid isPermaLink="false">http://blog.slaunchaman.com/?p=216</guid>
		<description><![CDATA[In a previous post, I]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://blog.slaunchaman.com/2009/11/01/cocoa-tutorial-strip-non-alphanumeric-characters-from-an-nsstring/">previous post</a>, I showed you how to trim non-alphanumeric characters from a string. Here I&rsquo;ll go more in-depth and show a method that I wrote to restrict text entry in a <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html"><code>UITextField</code></a> to alphanumeric characters. Since I also wanted the characters to be uppercase, I&rsquo;ll also ensure that only uppercase characters are allowed.</p>
<p>This should all happen in the <em><code>-&nbsp;(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string</code></em> method of your <code>UITextField</code>&rsquo;s delegate (which, of course, must implement the <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html"><code>UITextFieldDelegate</code> protocol</a>). I&rsquo;ve implemented it as follows:</p>
<blockquote><pre>- ( BOOL )textField:( UITextField * )textField
shouldChangeCharactersInRange:( NSRange )range
  replacementString:( NSString * )string
{
    /*
     * We only want uppercase letters and numbers in this text field, so if
     * this method is adding something else, we don't want it. But we also
     * want to support copy-and-paste, so it's not always going to be one
     * character added.
     */
    BOOL shouldAllowChange = YES;</pre>
</blockquote>
<p>The <em><code>shouldAllowChange</code></em> variable is set to <code>YES</code> initially because we <em>want</em> to allow this change when possible. The method will test the string to see if it meets criteria for rejection as we move forward.</p>
<blockquote><pre>    NSMutableString *newReplacement =
    [[ NSMutableString alloc ] initWithString:[ string uppercaseString ]];

    if ( ! [ string isEqualToString:newReplacement ]) {
        shouldAllowChange = NO;
    }</pre>
</blockquote>
<p>First, we define <em><code>newReplacement</code></em>. It&rsquo;s an <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html"><code>NSMutableString</code></a> so that if we discover non-alphanumeric characters in it, we can remove them on-the-fly. It also serves as a convenient string against which we can test to see if <code>string</code> is already uppercase.</p>
<blockquote><pre>    NSCharacterSet *desiredCharacters =
    [ NSCharacterSet alphanumericCharacterSet ];

    for ( NSUInteger i = 0; i < [ newReplacement length ]; i++ ) {
        unichar currentCharacter = [ newReplacement characterAtIndex:i ];

        if ( ! [ desiredCharacters characterIsMember:currentCharacter ]) {
            shouldAllowChange = NO;
            [ newReplacement deleteCharactersInRange:NSMakeRange( i, 1 )];
            i--;
        }
    }</pre>
</blockquote>
<p>In this section, we define the <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSCharacterSet_Class/Reference/Reference.html"><code>NSCharacterSet</code></a> that we want to work with - in this case, the alphanumeric character set. We go through one character by a time and if the current character isn&rsquo;t alphanumeric, we remove it from the <code>NSMutableString</code> (decrementing <em><code>i</code></em> so that we don&rsquo;t inadvertently skip a character) and set our <code>shouldAllowChange</code> flag accordingly.</p>
<blockquote><pre>    if ( shouldAllowChange ) {
        [ newReplacement release ];
        return YES;
    } else {
        [ textField setText:[[ textField text ]
                             stringByReplacingCharactersInRange:range
                             withString:newReplacement ]];
        [ newReplacement release ];
        return NO;
    }
}</pre>
</blockquote>
<p>To finish, if <code>shouldAllowChange</code> is still true, we return <code>YES</code> and allow the replacement characters to be added. Otherwise, we return <code>NO</code>, but not before using our replacement replacement string (say that ten times fast) to manually edit the text field&rsquo;s text. The end result is a text field that will consist only of uppercase letters and numbers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.slaunchaman.com/2010/02/26/cocoa-touch-tutorial-stripping-non-alphanumeric-characters-on-entry-in-a-uitextfield/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Cocoa Tutorial: Strip Non-Alphanumeric Characters from an NSString</title>
		<link>http://blog.slaunchaman.com/2009/11/01/cocoa-tutorial-strip-non-alphanumeric-characters-from-an-nsstring/</link>
		<comments>http://blog.slaunchaman.com/2009/11/01/cocoa-tutorial-strip-non-alphanumeric-characters-from-an-nsstring/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 16:36:46 +0000</pubDate>
		<dc:creator>Jeff Kelley</dc:creator>
				<category><![CDATA[Programming Tips]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[convenience]]></category>
		<category><![CDATA[NSCharacterSet]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://blog.slaunchaman.com/?p=192</guid>
		<description><![CDATA[Let’s say you have an]]></description>
			<content:encoded><![CDATA[<p>Let’s say you have an NSString that contains both alphanumeric and non-alphanumeric characters and you want to strip the non-alphanumeric characters out of it. The hard way is to manually go through, character-by-character, and put the character in a new string if it matches certain criteria. But why do it the hard way?</p>
<p>Apple provides a class that we can use for this to great effect: <a title="NSCharacterSet Class Reference" href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSCharacterSet_Class/Reference/Reference.html"><code>NSCharacterSet</code></a>. We want alphanumeric characters, so we can create a character set of the characters we want using this method:</p>
<blockquote><p><code>NSCharacterSet *alphanumericSet = [ NSCharacterSet alphanumericCharacterSet ];</code></p></blockquote>
<p>Now we have a character set like we want. We just need a way to turn our string into a string that contains only those characters. Unfortunately, the closest thing in <code>NSString</code>’s implementation is the <code>-stringByTrimmingCharactersInSet:</code> method. But that seems to do the <em>opposite</em> of what we want. Fortunately <code>NSCharacterSet</code> has our back here. We can use the <code>-invertedSet</code> method. So here is our final code:</p>
<blockquote><p><code>NSString *beginningString = @"Some string with non-alphanumeric characters. !@#$%^&amp;*()";<br />
NSCharacterSet *nonalphanumericSet = [[ NSCharacterSet alphanumericCharacterSet ] invertedSet ];<br />
NSString *endingString = [ beginningString stringByTrimmingCharactersInSet:nonalphanumericSet ];</code></p></blockquote>
<p>In this example, <code>endingString</code> will be equal to “Somestringwithnonalphanumericcharacters”.</p>
<p><strong>UPDATE:</strong> As it turns out, this only works if the non-alphanumeric characters are at the beginning or end of the <code>NSString</code>. Whoops.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.slaunchaman.com/2009/11/01/cocoa-tutorial-strip-non-alphanumeric-characters-from-an-nsstring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
