HMTimer

HMTimer is an Objective C wrapper that provides a simple, NSTimer-like interface for dispatch_after() with a block completion handler.

Usage

Unscheduled instances of HMTimer must be fired manually:

Scheduled instances of HMTimer are fired automatically upon initialization:

If the repeats parameter is true, the timer will fire repeatedly using the duration as the interval between firings:

You can specify a different dispatch queue to keep HMTimer from running on the main thread:

An instance of HMTimer can be invalidated by calling the - invalidate method:

Grab the code on Github

Figuring Out Error Messages in OSX

This post was originally published on the Heuristic Media blog on August 27, 2011.

One of the minor annoyances in OSX is its error messaging. We’re all used to seeing error messages like:

The operation can’t be completed because an unexpected error occurred (error code -1309).

Thanks. That’s helpful.

The message tells us nothing, but it turns out that it’s pretty easy to learn more from the error number. All you have to do is type one line into Terminal. Open Terminal (in Applications > Utilities) and type the macerror command followed by the error number:

macerror -1309

The macerror command for error -1309 returns:

Mac OS error -1309 (fileBoundsErr): file’s EOF, offset, mark or size is too big

This gives us a hint that the file we’re trying to work with is actually too big for the filesystem to handle (a very big file indeed considering that the old Mac HFS filesystem had a file size limit of 2 gigabytes, FAT32 is limited to 4GB and Mac HFS+ is limited to 8 exabytes – so big as to be practically unlimited). We can’t do much about the filesystem limit, but at least we can stop banging our heads against the desk trying to figure out why that file won’t open.

The macerror command may not decipher every error number you encounter. If it fails, you can always search for the error in MacErrors.h.

Give these tools a try next time you run into a mysterious error. You’ll save yourself some time and headaches and you might learn a little more about your Mac.

iOS Dev: Decoding PHP URL Encoded Strings with NSString

This post was originally published on the Heuristic Media blog on June 14, 2010.

If you do any PHP development, you should be familiar with the urlencode() function:

Notice that the resulting string contains percent escapes in place of the apostrophe (‘%27′) and exclamation point (‘%21′), but the spaces have been replaced with ‘+’ symbols. This is what urlencode() does, and it works just fine if the encoded string will be decoded by the urldecode() function in PHP.

Things get a little more complicated if you use urlencode() to encode a query response for an iOS app. The standard, easy way to remove percent escapes is the NSString stringByReplacingPercentEscapesUsingEncoding: method. When used on the encoded string shown above, we get this:

Oh crap. Why are the ‘+’ symbols still there?
Read more…