iPhone Programming: Protocols

One of the first topics that gave me pause during my iPhone development stint was the concept of protocols. Protocols are a strange & interesting cross between interfaces and event definitions. In many other languages ( Java, C#, VB.NET, etc… ) when you define an interface, you’re defining a common set of methods that objects must implement if they want to be considered to “implement” a given interface. One of the requirements of implementing an interface is that you must implement every method in the interface, either directly or via a subclass by marking the method as abstract.
Interfaces are use frequently for implementing “event callbacks”. Class A would accept an object, instance of a class implementing Interface B. Class A would then call methods defined by the interface, notifying the passed in object of certain events. Below is a simple example:

public interface MyEventInterface {
EventHappened();
}
public class EventHandler : MyEventInteface {
public void EventHappened() {
// Take some action
}
}
public class EventCaller {
private MyEventInterface _eventInterfaceObject;
public EventCaller( MyEventInterface object ) {
_eventInterfaceObject = object;
}
public void SomeCoolMethod() {
_eventInterfaceObject.EventHappened();
}
}

.NET languages aside, this was a pretty standard way to implement a poor mans event-handling mechanism in your code.

Note: .NET CLR languages like C# & VB.NET support a more formal definition of an event and delegates. These are not the delegates we’re taking about in this post.

Well, in Objective-C, protocols provide a similar function to the above. You basically define a standard set of methods/events, called a protocol, that your class will call. Then, when instances of your class are created, a class implementing your protocol, called a ‘delegate’, can register itself as a handler. Take the following protocol definition below:

@protocol MyProtocolDefinition
@required
- (void) Save:(NSString*) stringToSave;
@optional
- (void) OptionalMethod:(NSString*) parameter;
@end

The above code indicates that the Save() method is a required method to implement in classes implementing this protocol. However, the OptionalMethod is optional, meaning classes wanting to use this protocol only need to implement the Save() method, but they could also implement the OptionalMethod. It’s a pretty interesting concept and very powerful.
The iPhone SDK is built on protocols and delegates, which makes this concept critical to understanding how to hook into various parts of the iPhone SDK. In fact, when you create a blank Cocoa Touch project in XCode, the first class you’ll see is a AppDelegate.m file. Make sure to check out the Apple Objective-C Tutorial PDF for more information.

Getting Started w/ iPhone Development

Having spent the last few weeks doing iPhone development, it’s been quite a culture shock. I expected to have a learning curve with regards to Objective-C. My last experience with C/C++ was back in college and I never did any GUI programming in it. I knew that XCode was pretty powerful and Apple had made Interface Builder to aid developers in building UIs. So I thought I would be able to flounder around and pick things up quickly. After all, I wasn’t doing some sort of cool game, I was just doing plain old data entry apps. So lay out the UI, wire it up behind the scenes and boom, you’re done. Right?

Not so much.

While it’s true that Apple provides a GUI building tool, it’s pretty bare bones and is really meant for extremely basic applications. For example, take the follow screenshot from the awesome app Trapster:

photo.jpg

The UI above is actually non-trivial. The entire screen is a subclass of something called a UITableViewController. The grouping of sections with headings is officially supported out of the box, but the rows that contain a label and a textbox actually require a custom sub class of UITableViewCell. You need to specify geometry coordinates, spacing, etc… Not that it’s a HUGE task, but non-trivial and certainly unexpected, perhaps naively, by me. You end up having to write a ton of boilerplate code and making it reusable should be a high on any iPhone developers priority list to avoid cut-&-paste coding.

So, in order to give back to the iPhone SDK community that’s helped me so much over the past few weeks, I’m going to be posting some generic pieces of code that show how to do common things that you see in many apps, but that are not necessarily straight forward.

Mac OS X SVN Clients

I love using SVN. It’s free, fast, stable and it integrates with a wide variety of applications. DVCS systems may be on the rise, but right now, SVN is the ruling class for a lot of us out there. I don’t mind using the command line, but SVN has a wide array of options and it’s easy to mess up your typing and screw some things up. So, I’ve been playing with different SVN options on my main development platform: OS X.

So tell me this: why are there no affordable, native Subversion client for OS X? I could only find 4 standalone, native SVN clients:

Cornerstone – $69

Versions – $53

ZigVersion – $99

svnX – Free

( Note #1: XCode also supports svn for XCode projects, but it’s not a standalone client )

( Note #2: Textmate has svn support, but only for Textmate projects and Textmate costs $55 )

Now, with the exception of svnX, look at the prices for the above clients. $53 is the cheapest. $53+ for an SVN client?!?! That’s just unreal. Also, development of svnX looks a bit stagnated ( it hasn’t been updated in almost 1.5 years ) and doesn’t even support properties.

Whenever I do any .NET development on windows, I love using TortoiseSVN. It’s the one app on windows that truly feels integrated. It’s stable, fast and above all, free.

Why can’t there be anything comparable for the Mac?  

Anyone have any options I’m missing?

iPhone Theme for Three's A Pattern

Since we launched Three’s A Pattern back in January, we’ve been busy customizing and configuring the blog installation to make it more robust. One of the things we wanted to do was make the site iPhone friendly. However, we didn’t want to spend a lot of time customizing CSS and theme files.

Well, WPtouch takes care of pretty much everything for us. WPtouch is a WordPress plugin that automatically discovers when a user is browsing your site on an iPod Touch / iPhone and it renders your site using a customized theme. Out of the box, the features are very impressive. Take a look at the current Three’s A Pattern homepage viewed on an iPhone:

WPtouchTAPSS.jpg

The page is loaded with AJAX, so everything you see here responds to interaction using javascript. Take a look at what happens when you tap Search:

WPtouchTAPSearchSS.jpg

and categories:

WPtouchTAPCatSS.jpg

If your users hate this theme, there’s also a link right on the themes homepage that lets users view your regular theme.

Of course, since it’s a plugin, everything is customizable. However, the current version is slightly painful to upgrade if you’ve customized the plugin. The developers know this and are working on adding hooks to make the upgrade path that much easier.

Did I mention that it’s Free? Donations are appreciated. If you have a WordPress blog that you want to display on an iPhone / iPod Touch, I highly recommend checking out WPtouch.

xkcd statistics humor

xkcd is an online comic that typically deals with science and technology. I saw todays comic and had to laugh out loud.


xkcd is a great comic and it’s almost all due to it’s simplicity. Simple stick figures & no color. Just sharp, topical and witty writing.

March Chicago Semantic Web Group

Yes, it’s that time of the month again. Next wednesday will be the March Chicago Semantic Web Group meeting. As usual, we’re meeting at the ITA at 200 S. Wacker.

We have a special guest speaker, Mr. Andrew Dougherty. Andrew is going to be giving a talk about his companys use of semantics. Below is a summary of his talk:

FRDCSA Temporal Planning and Inferencing for Task Management through the Verber, PSE and FreeKBS Systems. Andrew will show how he am uses PDDL2.1,PDDL2.2,PDDL3, and NPDDL temporal planners in combination with federated search over various sources including the FreeKBS knowledge based system (for world state maintenance and goal and dependency tracking) to develop a personal task management system, and it’s Emacs user interfaces. It is planned to integrate to various semweb systems.

If you can make it you either need to RSVP or drop me an email so I can get you on the security list. Hope to see some of you there!