Developing iPhone Applications - 5 things you should know
September 15th, 2009 Lou Biancaniello, Consultant (email the author)
So you think you want to develop software applications for the iPhone. Sure, this is no big deal, you’ve been writing Java or C# for years now. You transitioned to those languages from Visual Basic 6. Heck, you even took C++ in school. You’re certainly not the type intimidated by learning a new language. Really, how different can Objective C really be? Any of this sound familiar? It sure does to me, since that’s exactly what went through my head prior to writing my first iPhone application. I quickly, and sometimes not so quickly, found that the jump from Java to Objective C was not as seamless as originally anticipated. So, from my pain, and for your benefit, here is a list of 5 things I wish I knew going into iPhone development. I have ordered them from merely annoying (or amusing, depending on your disposition) quirks of the language all the way to massive paradigm shifts that will change the way you code. No, I cannot overstate that last part enough. And no, I swear I am not a scare-monger…
So, now to the list:
1. Say goodbye to the “dot operator”.
Yes, we have all become very familiar with the normal way of calling methods on an object:
myObject.someGreatFunction(p1, p2);
Well, not so fast. Objective C has its own way of “sending a message to an object”. Same lovely object and method call in Objective C:
[myObject someGreatFunction: p1 parameter2:p2];
You may have noticed the extraneous “parameter2″ label in there, and yes, that is an excellent observation, which will make more sense in the next session. Now, like any good programming language, there is of course an exception to this rule, which is, a defined property can be invoked using the dot operator as you would expect, or by sending a message to the object using the standard java style getProperty/setProperty convention. Hence, if we had a property called “Name”, we could access it through either of these methods:
[myObject getName] or myObject.Name
2. So you think you know how to overload a method?
So I did promise last section to explain those crazy method signatures, well, here we are. Objective C does not have the same concept of method overloading as most developers are used to. For example, say we had a function that would create and return a Widget object, and has several overloads to pre-populate with several properties. In java:
public Widget createWidget(int prop1)
public Widget createWidget(int prop1, int prop2)
This gets a bit hairy in Objective C, as the parameter list is actually considered part of the method name. This requires the properties to be named, and are eventually compiled as the method signature. Ignore the dash, I will explain that later, also ignore the asterisk, that designates the Object is a pointer type and may be the topic of a future blog…
- (Widget *) createWidget: (int) prop1
- (Widget *) createWidget:(int) prop1 property2:(int) prop2
This can be confusing, and definitely takes some getting used to.
3. So you think you know what an Interface is?
Of course you know what an interface is, it’s just that in Objective C they call it something completely different, and have a construct called an Interface which sort of does what a Java Interface does, but not really. Confused yet?
In Objective C, a Class is declared in a very C sort of way. There is the header file, designated by a .h extension, which defines the Properties and Functions of the class. Then, there is the Implementation, designated by .m (don’t ask), which has all the good stuff. So, an Interface is sort of the same, as it defines a contract for the Class to follow. The bad part is, it cannot be shared: One Interface for one Implementation. Now, how can you define a contract that can be shared across Classes. There is a construct for that and it is called a Protocol. It behaves pretty much exactly the way you would expect a Java or C# interface to behave. Sorry, no Abstract classes though, no code examples in this section either…
4. Private? Protected? No.
I said I would explain the dash from earlier, and now I will deliver on that promise.
When a method signature is created, in both the Interface and Implementation, it is proceeded by either a “-” or “+”. The dash signifies that the method is an Instance method, and the plus means that it is Static. Simple enough:
Instance: - (void) doSomething;
Static: + (Object *) getSomething;
Now for some disturbing news. There is absolutely no concept whatsoever of Private or Protected methods in Objective C. Let me repeat that, there are NO Private or Protected methods in Objective C. Yes, everything is Public scoped (except instance variables, which can be private or protected).
5. Managed code? We don’t need no stinkin’ managed code!
So now comes that which will give you the most headaches and sleepless nights when developing in Objective C. If there is one area I could urge you to spend as much time as possible reading about upfront, it would be this. Objective C is NOT a Managed language, as least when it comes to the iPhone. What does this mean for you, if you don’t know already? This means there is NO Garbage Collector. You as the developer will be responsible for allocating your own memory, and are thus responsible for cleaning up said memory when you are finished with it.
What this means? We spoke briefly earlier about pointer types. This is any type that is not native and requires a dynamic amount of memory to be allocated. So, when we performed operations on our mystical Widget object earlier, we would need to create one as such:
Widget w* = [[Widget alloc] init];
Alloc is the command that allocates memory for the Object and think of init as the Constructor. So far so good? Well, remember that every time you allocate memory, you must also deallocate it. In the case of an instance variable this can be quite simple, there is a dealloc function that you can override, and use this very easy command:
[w release];
This starts to get tricky though when you have function scoped variables, and becomes even more difficult when you create an Object in one method or object, but pass or return it to another. It becomes very tricky to determine when and where to release your object. Too soon, and you could cause a crash when attempting to read a released memory address. Wait to long, and you lose the pointer and now have a memory leak. Luckily, there is great tooling in xCode for detecting and repairing leaks, but having a strategy around memory allocation upfront will go a long way towards making development simpler. Also, a piece of advice, don’t wait until the end to clean up memory leaks, do it as a part of regular development. Trust me.
So, is this at all a comprehensive list of all the little annoyances, idiosyncrasies, and game changers you will find in Objective C? Not by a long shot. I would encourage anyone to add comments with things they found difficult, or any tips/tricks they found to help ease the transition from Java or C#, or the more mainstream OO style languages in use out there. This is also not to say that there isn’t a host of great things that you will find in Objective C. I could just as easily do a 5 Things Objective C Does and Java Should Do Too list, but, perhaps another day.
Entry Filed under: Agile and Development






11 Comments Add your own
1. Sam Griffith Jr. | September 15th, 2009 at 2:05 pm
There are private and protected instance variables. Additionally you can declare a private interface inside your implementation file (.m) and those methods will be private essentially. You can read more here:
http://macdevelopertips.com/objective-c/private-methods.html
2. Narinder Bansal | September 16th, 2009 at 2:23 am
Nice Article. Your 5 points are mantras for a developer from java platform shifting to objective C. Dont know why objecitve C completely different from other languages. And no garbage collection for iPhone is big headache. Hope they will find a solution sooner or later. Thumbs up to this blog
3. Martha Habich | September 16th, 2009 at 8:43 am
Well written article. Thanks for the tips!!!
4. iphone programming | September 16th, 2009 at 2:10 pm
Great points. Well written. Concise! If readers would like to delve deeper into the iPhone Programming topics, then do have a look at the iPhone Training Program offered by the folks over at EDUmobile.ORG . You will master the iPhone SDK within 10 to 12 weeks flat, after which you can start putting your iPhone Apps on the App store! They’ve got a special promo code going now that will give you 25% off, you just need to Google it for now to get it… its floating around somewhere ($50 off)…
5. Jim Connell | September 16th, 2009 at 2:17 pm
This is a great list of major differences for Java/C# programmers. Those 5 were the same I found to be most different from Java as well.
I wanted to call out one inaccuracy in section one. Properties define accessors and mutators slightly differently than is typical in Java — the accessors are not prepended with “get”. In your example, the standard way for a property called Name is:
accessor: [myObject name] or myObject.Name
mutator: [myObject setName:aName] or myObject.Name = aName
Also as you’re aware, camelCase is preferred for property names.
6. Lou | September 16th, 2009 at 2:28 pm
Note from editor/writer… Jim, nice catch. The property would be accessed simply with the name, minus the get. I think it shows my point that this is a definite gotcha for Java developers.
7. Rudi Angela | September 17th, 2009 at 12:26 am
You make it sound as if the Obj-C makers have reused the term ‘interface’ in a different way than what is common. But there you forget that this term was already in use long before Java was born, to denote the interface of a specific software module or: how to talk to that piece of software.
The Java makers took that term to denote a more abstract concept. A kind of contract to which two sides should agree: the user of that interface on one side and a software implementation at the other side. A common word for such a ‘contract’ is ‘protocol’. So, after all, not bad names for the concepts they denote.
8. Steve | September 25th, 2009 at 3:17 pm
Moo.
9. grensley | December 2nd, 2009 at 3:24 pm
*shudder* now i don’t want to do it.
10. Lou | December 2nd, 2009 at 4:39 pm
Just as a response to several comments, it was definitely not my intent to scare anyone away from Objective C, or to put it down in reference to Java or C#. I was merely trying to point out some pretty major differences that developers from those languages may find a little difficult to grasp at first. I think that Objective C is a very good languages with quite a few upsides that would be nice to have in other languages.
11. Developing iPhone Applica&hellip | January 11th, 2010 at 10:11 am
[...] Memory management is core to any programming language, and it is always easier when you need not worry about it, like in Java or SmallTalk. But the ability to manage your own memory is a powerful aspect of Objective-C programming. When used correctly, Objective-C’s memory management using the retain/releases commands runs smoother and faster than Java’s garbage collection although it does place a much larger burden on the developer and introduces a much larger risk of memory leaks. As long as you strictly follow the simple rule, ‘You must take responsibility for releasing (i.e. de-allocating) any object that you have allocated or retained’, memory handling should be fine in Objective-C. Some background in handling memory elsewhere (like using malloc/free in C) can help expedite your learning but be aware of some interesting Objective-C specific syntax and concepts (read here). [...]
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed