iPhone Development for Mac Development Newbies

I recently switched to a Mac platform (for pleasure) and started developing on it, and even more recently started iPhone application development. I’d never seen (or heard of) xcode, cocoa, and those things. I’ve found the experience at times trivially easy (e.g. download/run/launch a sample in seconds, then learn from it’s code. Awesome) and sometimes frustratingly painful. Knowing what I know now, and wanting to help others avoid some of my mistakes, I’m planning a few simple postings.

Here are some tips I have for the newbie starting out:

  • Do bookmark and visit/re-visit the iPhone Dev Center. It is the place to start.
  • Do spend time on the Apple getting started videos, even though they do only scratch the surface. You need to get into the mindset that is iPhone before you start developing, and these videos help.
  • Find screencasts and/or  people to watch. I learned more useful tips (configurations, tool, keyboard shortcuts) by watching Kendall & Joe develop at iPhone Dev Camp, than I did from documents (however well written.)
  • Download and run/learn from the Apple Samples on the iPhone Dev Center. They are great. Not every one gives you perfect real-world examples but they help enormously.
    • UICatalog — examples of useful controls/transitions. Invaluable.
    • SQLiteBooks — sqlite, for those who want hand on DB examples. A bit bloated (with dehydration) but a solid. That said, I hear there are abstraction layers now.
    • GLPaint — good ’shake’ detection, and sound effects.
    • I could go on, I’ve downloaded 20 or more. Thank you Apple Developers.
  • Pick your style of application (e.g. utility one pager, tab based) carefully because what it sets up for you (including MainWindow.nib) is not fun to re-work. If you decide to change consider starting a fresh app and bringing in your existing code. [I went from non-tabbed to tabbed and got cranky w/ Interface Builder in the wasted hours that ensued.] This advice is good for Interface Builder editing also, especially when adding tabs i.e. delete and start a fresh tab don’t hack one.
  • Become comfortable with Interface Builder. Let me repeat that, no  increase it … conquer Interface Builder. It is your best friend, so long as yo don’t allow it to become your nemesis. My bad days were often spent ‘cos I hadn’t. It can be awkward at times so learn it’s quirks, and learn how to work happily with it. This excellent tutorial helped me: iPhone Interface Builder Tutorial.
  • Find other developers. Watch them, develop along side them, learn by osmosis. When your app is crashing (and it will) talk it through with somebody else (including your dog) it’ll likely help.

Some random comments:

  • Take time to learn Objective-C. It is C with more, and not C. It is C++ with less, and not C++. It is it’s own beastie. I’ve come to like it (I am glad it now it has @properties) but it isn’t like other languages and needs you to convert to it’s way of thinking.
  • Learn the rules for when to release an object. Learn retain/autorelease/release & be comfortable with them … before you do too much. You don’t want to be chasing memory leaks/memory corruptions. [My simple understanding of autorelease is that the object is released once that events stack unwinds to the top, and the thread goes back to a waiting state. Meaning it is there for a brief period, so if you need to stash something on a longer lived class (e.g. a view) remember to use retain & release in the view destructor. @property (retain) is your friend here.
  • You can (and usually do) have properties of the same name as the variable. Using self.x calls a property (and will do automatic release/retains for you if requested) whereas x calls the variable, and does not. Big difference.
  • Put a global breakpoint on objc_exception_throw, it'll give you a view of the stack before the crash occurs (i.e. as exception is being thrown) which is helpful for debugging.
  • Learn OPTION/COMMAND/UP ARROW to go from the implementation (.m) to the declaration (.h).
  • Visit the online class documentation when you work, it is well done.
  • Utilize completion via ESC, it helps you learn the available methods (messages you can send).
  • Create a view (controller) in Interface Builder, create the view controller classes, then immediately go back set the File owner as this class (COMMAND 4) and then (as immediately) set the view outlet on the file owner to point to the view. See the above tutorial. Forgetting to do this causes confusing crashes when you use the loaded view/view controller.
  • Never create two .NIB files with the same name in different groups. I did (by accident, one was empty) and I suffered with random crashes (in some cases the empty one would load.) Ugh, that cost a day or more & was miserably to debug.
  • Start simply, stick to UI classes, avoid things like threads until you've made good progress on the UI. I didn't, and had random crashes for a long time until I removed that code. Background threads can wait, leverage the existing Threads/Run Loops the I provides (e.g. your main loop.) KISS.
  • Go look at the UICatalog sample (run it) when you are designing your views.
  • Avoid UIButton and use the Apple build in icons/buttons and UIToolbar. Much nicer effect.
  • Create a Default.png splash screen for your app by taking a screen shot with Organizer once it is running. [I created my about page as a Model view, with no buttons but touch to hide, so I could screen shot it.]
  • When testing “shake” add a sound effect so you know it is occurring (even if your code does not show it yet.) Otherwise, like me, you might inadvertently try to hard and launch the device across the desk. Ouch.
  • Don’t ever (by mental slip) unplug the cable to the iPhone when you are installing an application, or (if you are unlucky, like me) you’ll have to spend the next period wiping it, restoring backups, and other non-fun stuff.
  • Do spend a lot of time on the Apple site (and Apple forums) but don’t forget to Google, and visit sites like Stack Overflow. More and more helpful content is coming out.

So, if you avoid the traps/pitfalls, have fun.

Please feel free to let me know if there are glaring mistakes in this. Remember, I am a newbie at this.

Here is what I am having fun developing

WildObs Observer iPhone Application

WildObs Observer iPhone Application

One Comment on “iPhone Development for Mac Development Newbies”


Doug Hays, January 19th, 2009 at 10:47 pm

Great write-up Adam! Nice collection of gotchas and tips that you wouldn’t get from Apple.

Thanks for going through the pains of learning for us.

Doug.

Leave a Reply