First adventures with iPhone programming

The last month or so I have been learning how to write apps for iPhone because I have a business opportunity for an iPhone app (which I can’t tell you about yet – stay tuned).

But this article is not about the app, it is about my first impressions of programming for iPhone after 20+ years of programming for Windows and UNIX. I have never even written anything for a Mac in all that time.

Coming from the world of C++, Java and C#, I am finding the Objective-C language rather alien. It’s like a bunch of LISP programmers took a look at C and tried to make it more like the One Pure Language. The result is like a bastard offspring of C and Smalltalk.

And it’s so wordy! To show what I mean, here’s an example of some code written in C++. This just manages the storage of a person’s name and can concatenate the first and last names.

using namespace std;
class Person
{
public:
// Storage.
string _first;
string _last;

// Constructor
Person(const char *firstname, const char *lastname)
: _first(firstname)
, _last(lastname)
{ }

// Concatenate the first and last names into a full name.
string toString() const
{
ostringstream oss;
oss << _first << " " << _last;
return oss.str();
}
};

Now I would have thought that C++ code has a lot of lines to do a fairly simple task but look at the Objective-C equivalent:

@interface Person : NSObject
{
// Storage.
NSString *first;
NSString *last;
}

@property (retain) NSString *first;
@property (retain) NSString *last;

- (Person *)initWithName:(NSString *)ifirst withLastName:(NSString *)ilast;
- (NSString *)toString;

@end

@implementation Person

@synthesize first;
@synthesize last;

// Constructor.
- (Person *)initWithName:(NSString *)ifirst withLastName:(NSString *)ilast
{
[super init];
if(self)
{
self.first = ifirst;
self.last = ilast;
}
return self;
}

// Destructor.
- (void) dealloc
{
[first release];
[last release];
[super dealloc];
}

// Concatenate the first and last names into a full name.
- (NSString *)toString
{
return [NSString stringWithFormat:@"%@ %@",self.first,self.last];
}

@end

The Objective-C requires many more lines of code to do the same job. You may say "so what?" but more lines means more time taken to write it and more places for errors to occur.

Also note that the C++ constructor is much simpler and I did not need to write a destructor at all for such a simple class. C++ is a very old language now, modern languages such as Python and C# do not require you to even think about destructors, they just manage the memory completely automatically. Having to care about allocating and freeing memory is kind of like stepping back into the 1990s.

The only place where the Objective-C was more elegant than C++ was the toString function where it could be implemented in just one line in Objective-C vs. 3 lines in C++.

But all grizzling aside, I would now call myself reasonably proficient at iPhone programming and am finding the API fairly easy to work with. My app is almost finished and I hope to have it in the App Store soon.

One thought on “First adventures with iPhone programming

Comments are closed.