Category Archives: iPhone

Don’t assume ARC solves all of your memory problems

You should absolutely be using ARC in your iOS projects, and if the project predates ARC, go ahead and use the refactoring tool to get it to ARC. It really doesn’t take long and you’ll end up with a more stable app that will be easier to maintain.

That being said, you can’t completely ignore memory management. You can still get EXC_BAD_ACCESS, Zombies, leaks, etc., even with ARC projects. Here are some things you should know

  1. ARC is not garbage collection. It statically analyzes your code and then puts in release and retain calls where they are needed. It’s still susceptible to a retain-cycle — two objects with references to each other. You can still have references to dead objects.
  2. If you have a retain-cycle, a common way to deal with that is to make one of the properties weak (which you should probably do), but now that reference is susceptible to becoming a Zombie. A weak property will not call retain on the object, so when the object is deallocated, it would then refer to a dead object. If you have weak properties and get EXC_BAD_ACCESS, go reproduce it under the Zombie instrument.
  3. Under ARC, you cannot use autorelease any more, but the calls into non-ARC libraries can (and do, especially iOS frameworks). This means that you sometimes need to use your own autorelease pool. Under ARC, use the @autoreleasepool keyword to wrap areas where autoreleased objects are created that you need released before you return back to the thread’s main pool.  If you see leaks of objects in Instruments that you don’t alloc or hold onto, and use threads, add in @autoreleasepool blocks.
  4. Don’t use non-ARC code in your project by copying the source in. Build them in their own Xcode projects and then use the resulting .framework or .a in your project. It’s likely you wouldn’t be able to anyway, but just in case. (if you happen to be MRC — then really don’t copy ARC source into your projects — this will usually be compilable code, but will leak like crazy)
  5. Test your code under the Zombie and Leaks instruments — especially if you use bridging, weak references, or are in any way managing retain-cycles (breaking them yourself without weak).
  6. It’s rare, but I ran into a bug in the iOS framework that didn’t retain Storyboard created Gestures correctly in a tabbed-app. It was my first big ARC project, and it didn’t even occur to me to check for Zombies, but that would have pin-pointed the issue right away. Rule of thumb, the underlying code is still normal retain/release/autorelease based — debug it the same way you would have with Manual Reference Counting.

Further Reading:

Get iPhone programming tips in your inbox with my Beginner iPhone Programming Tips newsletter.

 

XKCD Tooltip Bookmarklet

If you read XKCD on an iPhone and iPad, there’s no way to see the tooltip text on the comic (usually the funniest part).

Here’s a bookmarklet to pop-up the XKCD tooltip.

javascript:{var l=document.getElementsByTagName("img");for(var i in l)if(l[i].src.indexOf("xkcd.com/comics")>0){alert(l[i].title);}}

Here’s the process for adding it your iPhone

  1. Go to this blog-entry on your iPhone
  2. Select and Copy the bookmarklet above
  3. Go to XKCD, bookmark it, and save the bookmark
  4. Edit the bookmark, change the Title to “XKCD Tooltip”
  5. Paste the bookmarklet to the URL of the bookmark

To use it

  1. Go to XKCD,
  2. Choose the bookmarklet from your bookmarks — it will popup the tooltip

Static Code Analysis for iPhone Apps

Starting with Habits 1.1, I started incorporating static analysis into my build. In my previous experience with things like Lint and FXCop — I had found the signal to noise ratio to be too low to be useful. It’s hard to believe, but scan-build is 100% signal — every single issue it flagged was legitimate and needed to be fixed. Now, keeping Habits free from issues is easy, since I only have to deal with one or two at a time.

There were a couple of times I thought it was leading me in the wrong direction, but it was right so often that I just trusted it, and it was right about those too. I had a particularly interesting case with a custom table cell, where I wasn’t releasing properly, and causing a crash when I dealloced the window. Scan-build helped me make sure I found that before release.

Cocoa date functions are crazy

I thought Java was bad. Took me a while to figure this out and googling didn’t help, so I am just putting this out there for the next person who needs this.

To get the number of days in a month:

+ (NSInteger) getDaysInMonth:(NSDate*)date
{
    NSCalendar * cal = [NSCalendar currentCalendar];
    return [cal rangeOfUnit:NSDayCalendarUnit
        inUnit:NSMonthCalendarUnit forDate:date].length;
}

Reddit thread on iPhone Development

There’s a great thread by an iPhone developer on Reddit.

Yay! It took several months but as of today I was able to search for my app and I saw it listed inside the app store. Now you may be saying “so what”, but if you have ever looked into the steps that this takes, you know it’s something to celebrate.My app is a very simple game, but I think I’ve learned enough during this process to distill some important lessons that may help you if this is something you’ve been wanting to do…

I wrote a similar post when I finally finished my iPhone App.

Start Habits not Resolutions

I saw this list of New Year’s Resolutions on the Did I Get Things Done blog (originally from Amazon)

  • Lose Weight
  • Get Your Finances in Order
  • Go Greener
  • Curb Your Vices
  • Get in Shape
  • Relax More
  • Pursue a New Career
  • Upgrade Your Technology
  • Organize and Optimize
  • Start a New Hobby


The main reason that I have had a problem with a resolution is that I don’t really think about them much a week or so after New Year’s. A few years ago, I created a small web app for myself to log how well I was doing at keeping to resolutions I was making. A few months ago, I ported it to the iPhone as
Habits.

Instead of making resolutions this year, I created a few habits instead. I want to lose some weight this year (the #1 resolution), so I added a habit to run every 2-3 days, to do bicep/chest and shoulder/tricep weight training once a week. I want to keep my house in better order, so I added a habit to clean up and to process my mail pile more regularly.

A lot of these resolutions should just be a recurring task that you try to do as often as possible.

Habits on Sale for $0.99 until the end of January

Habits is the perfect application to make sure that you stick to your New Year’s resolutions, so from now until the end of January, I am putting Habits on sale for $0.99.

I am working on version 1.1, and I will post it at the end of January and return it to its old price. Until then, here’s hoping that you’re able to turn your resolutions into habits.

(The AppStore takes time to fully update — please make sure it says that the price is $0.99 before you buy)

Buy Habits on the App Store

O’Reilly’s iPhone AppStore Answers

iPhone AppStore Answers from O’Reilly’s Inside iPhone Blog. There are frustrations with the AppStore, but as O’Reilly acknowledges, Apple appears to be listening:

Changes have been relatively slow to come to the App Store. However, with the addition of review copies, as well as limiting ratings to those who’ve purchased applications, Apple has made changes that have been welcomed by developers. I’m hopeful that the App Store will continue to improve over time and address additional issues.

The other major improvement is dropping the NDA for released SDK’s, thus opening up the possibility of books, online tutorials and blogging about iPhone development.