I spent the start of 2013 working on my first iOS SDK. In no particular order, here’s some of what I’ve learned about Objective-C.
- The hash of a collection is equal to the number of elements in that collection
- Singletons (er, “shared singletons“) are an accepted pattern in Objective-C
- Categories are cool. They’re kind of like Ruby’s mixins.
- Class clusters defy expectations. You can’t cleanly override a method in a class cluster with a category.
- The community seems to be suffering from Apple’s control. Too many mentions of “I can’t tell you because of NDA” on StackOverflow
- Apple’s docs are pretty good… if you can find the right ones
- There’s no easy way to build a “fat archive” and static framework unless you start with this project template, which I didn’t.
- appledoc should be the de facto documentation engine now, but isn’t.
- Unit testing networking code is much harder than it should be.
- I miss Guava. Simple things like Objects.toStringHelper, Objects.hashCode, and EqualsTester.
- I miss the builder pattern. Objective-C loves telescoping
constructorsinitializers. Likewise, I miss Guice.
- Many of my favorite libraries for unit testing have been ported to Objective-C.
- The various abstractions for multithreaded programming are at war. GCD (the latest and greatest) vs NSOperationQueue (the most abstract) vs RunLoops. (Does this belong in this category? NSURLConnection can specify a runloop OR a queue, so I think so.)
- Some (but not all) of CoreFoundation is open source. A lot of NS* classes are the same as their CF counterparts via “toll-free bridging“
- Variable argument lists don’t know the number of arguments without being told. Most people use a nil-terminated list. A format string (implicit count) or explicitly passing the count also works.
- There is no NullPointerException; sending a message to nil is fine (no exception raised). This can really bite you in the ass.
- There are best practices. Read them before you start your next (first?) iOS or Objective-C project. I found this after I figured them out in bits and pieces.
Since I was simultaneously writing an Android SDK, expect a similar post about what I learned about Android in the process.