Friday, January 2, 2009

A Win32 Developer's First Impressions of Programming the iPhone

Several friends have recently asked me what it is like to make the jump from programming Microsoft Windows to the Mac iPhone so I thought I'd write up my take.

Here is my other post on Mac and Windows development: http://damondanieli.blogspot.com/2008/08/my-current-development-environments-on.html

Note: I've only been programming Objective-C for two and a half months (as of today) so take this as raw "First Impressions" and not a definitive guide to programming the iPhone.

My Background

I come from a C/C++ background and have been writing Win32 applications since Windows 3.1 days. However, the last time I designed and wrote a Microsoft retail consumer application was a while ago:

Microsoft SideWinder Game Voice
http://www.microsoft.com/presspass/features/2000/aug00/08-24gamevoice.mspx

Game Voice was a niche product, but had good response from the gaming community:

From Penny Arcade:
"[SideWinder Game Voice] being my favorite Microsoft product of all time."
http://www.penny-arcade.com/2005/7/4/

What was fun about working on the SideWinder line of gaming devices is that they were built with a small team of programmers (2-5) where each developer had a massive impact on the product. I can't think of another Microsoft shrink-wrapped retail product which was written by less than a handful of developers.

Objective-C: Very First Impressions of Syntax

I didn't like the syntax of Objective-C at first.

This Objective-C code allocates and initializes an instance of a Topic class, assigns it to a local member called "postTopic" and then sets a flag that this is a personal topic:
Topic *t = [[Topic alloc] init];
self.postTopic = t;
[t release];
self.postTopic.isPersonal = YES;
I was confused by the bracket syntax (when to use brackets vs. when to use dots) and why it was customary to assign an allocated object to a temporary variable, then assign the temporary variable to a member of this instance, then release the temporary? I never liked putting "this" in front of member variables in C++ so why did I have to use the "self" notation when postTopic was clearly a member? And why did Apple use "self" instead of "this" anyway? And why was a boolean "YES" instead of "true" and so on...

Compared to something like this in C++:
postTopic = new Topic();
postTopic.isPersonal = true;
Or C#:
postTopic = new Topic();
postTopic.isPersonal = true;
Or Java:
postTopic = new Topic();
postTopic.isPersonal = true;
It was as if there were two tribes of programmers (C++ versus Objective-C) which evolved from a single tribe (The C Programming Language). Some things looked familiar, and some completely alien.

Objective-C: Second Impressions of Syntax

At first, I felt like I was taking a step backward from modern language such as Java or C# into the hinterland of C pointers, managing memory, and the like. But truth be told, I really like Objective-C now mainly due to the way it integrates with the rest of the framework.

There are a LOT of nice things that the Cocoa framework provides (messaging, run loop, great UI controls, etc) which make the job of writing a good, sophisticated application within the reach of a single programmer.

Setting aside some of the differences of opinion on things like "YES" vs. true and "self" vs. "this" (in other words, how everyone else does it versus how Apple does it), Objective-C makes a lot of sense when you start to use it in context of the iPhone.

Here is code to make a con
nection to a web server which will run asynchronously AND return status and information marshaled onto the UI thread via a delegate (very nice):
NSMutableURLRequest *req = [NSMutableURLRequest
requestWithURL:@"http://www.apple.com"];
NSURLConnection *conn = [[NSURLConnection alloc]
initWithRequest:req
delegate:self];
self.asyncConnection = conn;
[conn release];
The bracket notation allows you to send messages with named parameters to classes or objects which makes it much more clear what the parameters are.

The above code is more explicit than C++, which would be something like:

Connection *conn = new Connection(
"http://www.apple.com",
req,
ConnectionManager::ConnectionStatus,
this);
Also, the delegate parameter in the connection initialization call makes the "C++ callback to a static member of the class which then dereferences the this pointer to call an instance member of that class" syntax much cleaner and standardized across the framework.

There are probably a zillion pros and cons that other developers can cite, but what I liked was that it took away all of the goo associated with marshaling information onto the UI thread if you are on a network thread including automatically adding an "AddRef/Release" to any objects put in the message queue since all objects are derived from the NSObject class (which implements the retain/release methods required by the NSObject protocol).

Compare that to the Win32 pattern of creating a hidden window (then implement a system to allocate and free memory appropriately) to marshal information onto the UI thread from another thread or non-apartment component.

Transitioning to the iPhone

My transition to the iPhone SDK and Objective-C took about a month to "find my ass with both hands". The iPhone SDK has quite a few example programs, but not a comprehensive walk-through tutorial for people who are starting from scratch.

That, and the fact that there were not any books on the market yet on how to program the iPhone meant I had to use a lot of tutorials on the Internet with varying levels of completeness and quality of code.

Summary

So in short, if you come from a C/C++ background with experience in threading and UI development, the tra
nsition should take about a month to become proficient. I'm not the sharpest bulb in the shed so your mileage will probably be better.

If you come from a more modern language background (such as Java or C#), then it will probably a lot longer as you must learn about pointers and pay very careful attention to reference counts on your non-autoreleased objects. Whereas garbage collection take care of that for you in today's languages.

If you come from a web scripting language (Ruby on Rails, PHP, etc), then I have no idea of how long it would take to learn Objective-C. Not impossible, of course, but the ramp up of memory management plus the syntax of the language plus client UI development plus threading plus ... (the list goes on and on) would take some significant learning. If you have gone through this route, contact me and tell me your experience and what helped you learn.

Actual Results

Objective-C, the Apple SDK, Cocoa Touch and support frameworks enabled a single developer experienced with C++ Win32 APIs to go from complete Mac/iPhone/Cocoa novice to submitting
a fairly sophisticated networking application to the Apple iTunes AppStore in a month and a half. I also wrote and deployed a web service written in Ruby on Rails to back the iPhone application in that time, but that is another post on ASP.NET vs. Rails.

Although my first submission is still "In Review" for the AppStore, here are some screen shots of PhotoFeedd, an iPhone a
pplication which allows users to create topics, and everyone can post photos to that topic. BTW, It's free to download and a lot of fun so try it out.



































































4 comments:

Unknown said...

Ruby on Rails is the best web development framework.

Fast growing in popularity and is powering applications of some of the fastest growing companies like Twitter and Groupon

Unknown said...

Ruby on Rails Development affords Ruby on Rails offshore outsourcing web development and is based on open source web platform.

Ruby on Rails | Ruby on Rails Development| Rails Development

testing said...

I can see that you are genuinely passionate about this! I am trying to build my own website and you’ve helped me with some great information.

I would like to share this free push notifications apps on how to send push notification messages to unlimited iPhone devices for free via https://pushwizard.com, so that the readers here will benefit.

John said...

In the modern, fast-paced world we live in, technology has completely transformed the way we consume media. The days when we had to rely on traditional television sets to watch our favorite shows and movies are long gone. With the advent of smart TV and mobile technology, we now have the convenience of accessing TV listings and guides right at our fingertips. This has not only made our lives easier but also significantly enhanced our viewing experience. In this article, we will delve deeper into the integration of xmltv epg format on smart TV's and mobile phones, and how it has revolutionized the way we watch TV.