Text

Archiving, Unarchiving, and Copying NSManagedObject’s

Core Data is awesome. So awesome I use it in just about every single Cocoa application I write for either Mac OS X or iOS. What makes Core Data so great is that it normally just works. By normally I mean 80% of the time. The other 19% of the time there is a not-so-easy but still simple workaround but still a workaround that doesn’t take long to implement.

Then theres the 1% of cases where Core Data is so frustrating it nearly makes probably others and myself want to either run off a cliff, walk in front of a giant truck, rob an Apple store or commit suicide in any other way possible.

The 1% for me normally comes down to two scenarios. Copying NSManagedObject’s and putting them on the clipboard for copy/cut/paste sweetness.

If you’re in the same boat I have a treat for you today. 6 hours, 2 beers, a little research, a lot of documentation and a bit of programming and testing I created two classes and a category. NSManagedObjectArchiver, NSManagedObjectUnarchiver and a NSManagedObjectCopying category.

These three classes do exactly what you think. They archive managed objects to NSData, unarchive them from NSData and allow you to copy them. They not only work on NSManagedObject’s attributes they also support relationships fully ( including circular ones! ) so you can take a single object archive it and restore it later. They’re also smart though and only archive the object’s once even if it’s in multiple places in the object graph.

I should note these classes are not “extremely” tested and thus may have some quirks.

View Gist @ github

Enjoy, -Robert

Text

Using Uncrustify directly in Xcode 4!

One of the biggest problems I have using 3rd party libraries is that they’re formatted horribly and there’s not many great code formatters around that can do Objective-C, C, C++ all in one go. Luckily Uncrustify can but it’s not exactly the easiest to configure or get running easily. With the help of a small ruby script and a little time exploring the preferences in Xcode you can set up Uncrustify to scan your project’s directory for all .h, .m, .c, .mm files and format them at the whim of your keyboard. You’ll need two files stored somewhere on your drive. I put them in /usr/local/etc/.

Shell Script:

Uncrustify Config:

Xcode Behavior:

With both of these stored on your hard drive it’s as easy as going into Xcode’s preferences and adding a behaviour on the behaviour tab. Here’s what mine looks like:

Remember to customise the uncrstufiy config as you see fit!

-Robert

Tags: xcode4 objc
Text

Changing Xcode 4’s default code snippets

Xcode 4 brought along a code snippets feature that is actually useful. Nearly as useful as TextMate snippets but not quite as powerful. One of the most annoying things though is that Xcode bundles in a bunch of default snippets that you can neither edit nor remove in Xcode.

With a little snooping around it’s pretty easy to find how to update these or get rid of them so you can have your code looking the way you like again.

Is what you’re looking for. It’s a .plist and pretty straight forward. I tend to back up the System one and symlink my own from a Dropbox directory for all my replacements.

Note you shouldn’t remove this file though just empty it out if you’re looking to get rid of all the snippets.

-Robert