iPhone Application Development
For quite a while I have been itching to try out some iPhone/iPod Touch development. Being currently unable to afford a Mac of any form to try it out, I thought I would still be waiting however I was lucky enough to get to borrow a MacBook for a couple of days and give it a shot.
The first thing I noticed is how different Objective-C is to the other languages I know such as C# and Java; namely method calling and class definitions. Objective-C does not use the “dot” notation for accessing instance methods and variables (although it does use it for accessing properties), instead it uses the following structure: [myInstanceName someMethod];. This took some getting used to in the beginning, and while its less intuitive than the “dot” notation in my opinion, its only a minor annoyance that arises purely from my use of other languages.
As I mentioned previously, I also found the class definitions quite strange. Classes in Objective-C consist of two files, an interface file (.h) and an implementation file (.m). The interface file defines variables, method declarations and properties. The implementation file then “synthesizes” properties from the interface and fills in the method logic. Again this was something I found very strange compared to other object oriented languages and was a little bit more difficult to get my head around. Following is an example of a class in Objective-C and C# that does basically the same thing:
C#:
namespace MyFolder { public class MyClass { private string myString; public void SaveString() { // Do something to save the string somewhere } public string MyString { get { return myString; } set { myString = value; } } } }
Objective-C:
MyClass.h
@interface MyClass { @private NSString *myString; } - (void) saveString; - (void) dealloc; @property (nonatomic, retain) NSString *myString; @end;
MyClass.m
#import "MyClass.h" @implementation @synthesize myString; - (void) saveString { // Do something to save the string somewhere } - (void) dealloc { [myString release]; } @end
I must say that I find the C# approach to defining classes much better, however I’m assuming there is probably some reasoning behind Objective-C’s approach. I’ll post it if I find out what it is.
An important part of iPhone development also seems Apple’s Interface Builder. This rather nifty program allows you to drag and drop your interface elements and define connections between them and the code similar to the way WinForms applications can be made. I didn’t go too far into the Interface Builder myself and didn’t dynamically create any elements as I was more interested in getting comfortable with the language before taking that step. Apple’s iPhone Development Centre provides plenty of resources to help you out with getting started. In particular I found their “Your First iPhone Application” article very useful in getting started. It explains everything very well for most of the tutorial. However, since its a “Your first….” article, I had expected it to be step-by-step the whole way through. It missed out a key part of the tutorial when talking about dismissing the iPhone keyboard using a “Done” button; that is it doesn’t mention that you need to go into the Interface Builder and set up a connection between the the text field delegate and the File Owner in order for this code to work.
Having completed the “First iPhone App” tutorial, I started making a new project because I wanted to use a new class somewhere that wasn’t a View Controller or Application Delegate and also the chance to play around a bit more with the Interface Builder and try get comfortable with it. I made a simple application that receives a numerator and denominator then displays the fraction, decimal and percentage on-screen. It contained no data checking to ensure that only numbers were entered as the numerator and denominator however that would be a useful feature to add to get to grips with the language a bit more. Here is the source for my fractions application.
Some resources I used for beginning iPhone Development:
I would recommend taking a look at the Objective-C Reference at least before starting the “First iPhone Application” tutorial. I read upto “Property Declaration and Implementation” before moving on.
