Objective C Pitfalls for Java and C# Programmers

These were my first experiences with Objective-C, Xcode and the Mac

Can you read this, Luke Skywalker?
If you came from Java or C#, your most prominent errors or pitfalls with Objective C might be:

  • Pointers and the asterisk: http://stackoverflow.com/questions/1105815/placement-of-the-asterisk-in-objective-c
  • The type “id” is always a pointer, so no asterisk there
  • Strange effects happen when you “#inport” .h files with cyclical references. I didn’t find a better solution than having id instead of the Type I wanted to use in one of my classes
  • There are no null pointer exceptions. This means you’ll all the time miss the initialization of some object or some reference and won’t recognize it. Since recognizing this, I love null pointers exceptions. But this is language design in Objective C. So if you definitely need to be sure, throw an exception if you want to be sure and / or add some unit tests.
  • When the application crashes, you won’t get meaningful answers from the console often times. To get some more information you’ll need to set a breakpoint in the last thrown exception statement. You can find out more here.
  • When you want to use a simple integer like 5 and get the warning / error “makes Pointer from integer without a cast”, you need to use [NSNumber numberFromInt:5]
  • Always correct header file first, implemetation file second and always correct from top to bottom, as the compiler does this too. When the compiler sees an error in the header file for example, it might make up 50 more errors somewhere below. This is literally top-down debugging.
  • Don’t try to mess with XML too much yourself. Use this nice, free and fast XML framework instead.
  • There are great resources for iOS development out there, for example the full open source Canabalt game or framework-like libraries that could make some things easier, if you see it before you’ve implemented all such things yourself.
  • To find such resources, I’d read this blog and follow it’s writer on Twitter.
  • Oh yeah, and Google and Stackoverflow should fix the rest.

Creative Commons License photo credit: Stéfan

Related Posts:

, Posted Thursday, February 17th, 2011 under Programming, Tipps.

2 comments

  1. Three points. Firstly, you don’t want to use #include. Use #import. It helps deal with cyclic references. Secondly, with nil values, you want to either just check for nil in cases where you may get nil, or use NSAssert when you definitely shouldn’t. Exceptions aren’t really used in Obj-C, except for programmer error. As such the idea number of @try/@catch statements in your app code is 0. Finally, one area where exceptions can often happen is crashes. Be sure to turn on “stop on objective-c breakpoints” in Xcode so that the app exits to the debugger just before a crash.

  2. Thanks – fixed the import thing – never used import so far.

Leave a Reply