Rails: Finding Associated Objects Safely

This one’s simple but I’ve run across it a dozen times today so I’ve decided to vent my frustration via education (I think this is healthy…right?). You have the following simple models: class Customer < ActiveRecord::Base has_many :orders end class Order < ActiveRecord::Base belongs_to :customer end Now, almost invariably, in your OrdersController you have a destroy method that needs to ensure that Customers can only delete their own Orders. Assuming @customer holds the current user’s Customer record, don’t waste your time doing this:

iOS: Categories in a Static Library Not Working?

Grrr…if you have category methods in a static library and those methods don’t seem to exist at runtime (unrecognized selector sent to class 0x...), first make sure that your project target is passing the -ObjC flag to the linker (in Other linker flags). If that doesn’t fix the issue, change the -ObjC flag to -all_load. Details from Apple in this Technical Q&A. You might also try using nm to see if your category methods are being properly compiled into your .

iOS: Maintaining Compatibility While Leveraging New Frameworks

If you starting using a framework that didn’t exist in a previous version of iOS, you’ll get dynamic linker errors when attempting to run your application on older devices. For example: dyld: Library not loaded: /System/Library/Frameworks/EventKit.framework/EventKit Referenced from: /Volumes/(...) Reason: image not found To fix this you need to modify your application so that the framework is weak-linked, meaning that the application will continue to run even if linking fails.

Rails: Application-Specific Config File

If there’s one thing I hate in programming or web design, it’s having to repeat myself. So I always do my best to abstract out repeated bits of information and put them in a single, easily changeable location. A nice way to do this in Ruby on Rails is to create your own YAML config file. First create the config file in config/config.yml (or any other name), and then we’ll get it loaded.

NSPasteboard and Dynamic UTIs

When dealing with pasted or dragged data in Cocoa, we receive the passed data on an NSPasteboard. Prior to v10.6 (Snow Leopard), this pasteboard could only contain a single item, but now it can contain multiple items, which are returned as an NSArray via the -pasteboardItems method.

The data passed in via the pasteboard can be in a variety of types (for instance, a single image could be passed both in JPEG format and TIFF format), and for compatibility reasons these types can be expressed in various ways—they can be MIME types, UTIs, OSTypes, or PboardTypes.

If we want to handle pastes and drags in the proper, modern, multi-item fashion, you’d think we could just iterate over the -pasteboardItems (each is an NSPasteboardItem), ask each for its -types, and handle the data in our preferred format. Unfortunately things aren’t always that simple.

Interface Builder: Objects-Under-Mouse Menu

For those annoying times when you’re trying to select a particular object in Interface Builder and you keep getting an item above or below it (NSTableView, anyone?), try this: Control-Shift-Click to display a pop-up menu of all the items under the mouse.

Copy, Don't Retain NSString Properties

In general, @properties of type NSString* should be copied, not retained: @interface SomeClass : NSObject @property (nonatomic, copy) NSString* someText; @end The reasoning behind this is that because NSString is a class cluster, what appears to be an NSString could really be an NSMutableString, and thus could change behind your back—generally not something you want. However, if you specify copy then the instance of SomeClass will have it’s own unique copy of the original string.

First Post

Welcome to my new scratchpad for posting programming tips, gotchas, best practices, and revelations. I’m not intending to focus on production value but rather on jotting down things I’ve learned. I’m mostly working with Objective-C on iOS these days, so that will comprise the majority of the content. Happy hacking.

Focusing, Shuffling

I’m doing a bit of work to get things better organized around here. I have the both fortunate and unfortunate affliction of having far too many interests—the harm in this is that if I blog about what’s on my mind, there’s no way anyone else will actually want to follow along. From here on, this blog is going to focus on lifehacking, location-independence, and entrepreneurism. It’s about embracing your passions, eschewing societal standards, and living an awesome life.

Dreams, A Personal Insanity

1. It was kind of my friends to throw me a surprise party, but the jovial mood was tainted by the presence of my nemesis. I knew this stout black man from his neighborhood pool cleaning service but apparently he moonlighted as a cake baker. And tonight, not only was he baking my cake, he was extorting six dollars from me for a cake pedestal–whatever that was. Ah well–I brushed it off and returned to the party, replete with champagne and cubic leather ottoman.