Cocoa debugging tip

When you’re debugging a memory allocation issue, it’s very useful to be able to see the retain count of an object. One way to do it is by calling the object’s retainCount method, which you can do in the debugger console by entering something like call (int)[self retainCount].

That’s a lot of typing if you do it very often. Fortunately GDB lets you create macros that can make it a lot easier. To define a macro called rc, which will print the retain count, you can define a macro as follows:

define rc
call (int)[$arg0 retainCount]
end

You can now type rc self to see the current object’s retain count. Unfortunately you’ll have to enter that definition every time you start the debugger.

However, GDB lets you create a startup file with commands that will be executed every time it starts up. Simply create a file called .gdbinit in your home directory and enter that definition in it. You will now have the command rc available every time you use the debugger.

iPhone app update & install bugs

AppleTell writes about one bug involving application updating on the iPhone, which I can confirm.

The problem is with how iTunes installs app updates. Like I said, I had already updated Cro-Mag Rally once before. What I didn’t realise at the time was iTunes didn’t actually overwrite the original file—it creates a duplicate! Yes, you read correctly. iTunes created another file in the Music/iTunes/Mobile Applications folder called “Cro-Mag 1.ipaâ€? to sit along side the original ‘Cro-Mag.ipa’ file. I checked the info of the app currently in my iTunes library at that time, and sure enough, it’s file name was “Cro-Mag 1.ipa.â€? Now we’re getting to the problem. You see, when iTunes is checking for app updates, it must be going by file name rather than app name. So, in this case, iTunes has been
checking for updates for “Cro-Mag 1â€? instead of “Cro-Mag,â€? and, of course, it won’t find any.

To prove this, I removed Cro-Mag Rally from the iTunes Library, but kept the file. Then I deleted the original “Cro-Mag.ipaâ€? file and re-named the other, removing the “1â€? from its name. Then I added it back to iTunes with its new name of “Cro-Mag.ipaâ€? by dragging it to the iTunes window. Immediately, I had the message at the bottom of the window telling me of an update available!

However, I’ve found two much more serious problems.

Applications purchased in iTunes are handled differently than applications purchased on the iPhone. If I purchased an application in iTunes, I can only update it from iTunes. If it was purchased on my iPhone, I can only update it from the App Store on my iPhone.

Far more serious, I find that apps I purchased on my iPhone aren’t being backed up to my Mac and can’t be managed from iTunes. If I try to delete one of those applications from the iPhone, it warns me that the application hasn’t been backed up. Has anyone else seen this?

Document-based vs. event-driven XML parsing

When I wrote my FriendFeed Cocoa classes, I used the document-based NSXMLDocument class, which I later found out isn’t available on the iPhone. However, there’s an alternative that’s available both in Mac OS X and the iPhone: NSXMLParser, which uses an event-driven approach.

While the two approaches are very different, I found that it isn’t too difficult to rewrite my code to use XMLParser. I’ve been working on a fun iPhone app using the Flickr API and I found that the event driven approach is very easy to use.

With NSXMLDocument, I called the parser and then iterated through the resulting collection of elements, creating my own objects from each element. With NSXMLParser, on the other hand, my callback methods get called for each element, which I can then use to create my own object in a very similar way. A single element with several attributes, such as Flickr’s response format, is very easy to deal with since it can be handled in its entirety with a single method. An element with child elements, such as FriendFeed uses, is only slightly more difficult. I need to use both the start element & end element methods for the parent element.

After I finish the Flickr app, I plan to rewrite my FriendFeed class to work on the iPhone.

Spaces & Window Management

I’ve noticed a strange problem on my MacBook Pro where the Finder wouldn’t bring its windows to the front when I click on it in the dock and ‘bring all to front’ didn’t work in any application. For a while I thought one of my login items or extensions was responsible for it. When I logged in as a different user, everything worked normally.

It turns out the problem is caused by turning off the option ‘When switching to an application, switch to a space with open windows…’ option in the Spaces preferences. Once I turned it back on, everything started working normally.

Expose+Spaces.jpg

iPhone 3G sold out everywhere

According to Apple’s iPhone availability checker, the iPhone 3G is sold out at every Apple store in the US (except 8G at the Kahala store in Honolulu, HI and 16G white at Rockingham Park, NH & Fifth Ave NYC).

Last week in Vancouver I saw lines of people waiting to buy iPhones at every Rogers & Fido store, including a kiosk in Pacific Centre. The iPhone 3G appears to be selling even faster than the original model.

iCrapps

After my last post, I decided to create an entire site dedicated to bad iPhone application reviews, done with humor. If you want to gripe about the iPhone crapplication you just bought, come to iCrapps.com. If you’d like to post your own application reviews, contact me and I’ll sign you up as a contributor.

My first victim is Go Figure Lite. Enjoy!