Archive for April 15, 2011

Friday, April 15, 2011

Compile-Time Tips and Tricks

Mike Ash:

With a bit of trickery, it’s possible to build a check which happens at compile time, late enough so that types are known, but before your program actually runs. There are actually a few different ways to do this, but my preferred way is to declare an array whose size depends on the expression to test. If it passes, set the size to 1, which compiles. If it fails, set the size to -1, which is illegal and causes an error. The error message cannot be fully customized, but by giving the array a descriptive name, the message can still be conveyed.

Cutting That Cord

John Gruber:

You buy books on your device, you read them on your device, and your history, bookmarks and other metadata all get synced to your iTunes account in the cloud. And it works great. But a lot more apps should work like this. Should wireless Safari bookmark syncing cost $99 a year? Shouldn’t it be easy for iOS game developers to sync progress for the same game across multiple devices using the same iTunes account? App Store developers shouldn’t have to rely on another third-party — Dropbox — for this sort of functionality.

The Proper Care and Feeding of NSImage

Paul Kim:

You could try turning off image interpolation in the destination graphics context but this isn’t always possible or desirable. The better solution is just like before: use a custom image rep to do the drawing (select “Drawn (custom image rep)” from the pop-up). Since the drawing occurs at drawing time, instead of image creation time, it knows about the context it is drawing into and therefore can provide your drawing code with a context at the correct resolution. The crisp square on the right speaks for itself.

Fortunately, blocks make it easy to use custom image reps rather than -lockFocus.

Subclassing NSInputStream

BJ Homer (via Dave DeLong):

-[NSInputStream _scheduleInCFRunLoop:forMode:] is the equivalent of CFReadStreamScheduleWithRunLoop for your stream. Do whatever you need to do so that you can give proper kCFStreamEventHasBytesAvailable notifications (and any other notifications requested) at the proper time. That may involve scheduling a timer on the run loop, or if your subclass is just wrapping a vanilla NSInputStream, simply scheduling that stream on the run loop. Implement this method as if you were implementing CFReadStreamScheduleWithRunLoop for your stream.