A Service to make Objective-C “description” methods easier
Ok, so I still write Mac apps in Objective-C. I swear I’ll buckle down and get more comfortable with Swift eventually, but for the time being, I’m mostly square brackets.
In that spirit, I wanted to share a simple Automator Quick Action I created to make adding description
method overrides to your Objective-C classes a little simpler.
Back in the day when Xcode plugins roamed freely, there was one from Adam Smith that did exactly this (and was probably a more complete solution). I haven’t found one that did the same since Apple updated Xcode’s plugin architecture to extensions and moved them all to the App Store.
Download the Service below and double click it to install. Once installed, you can open a header (.h
) file and select all the text, right click, and run Xcode Describe
from the contextual menu. The resulting description
method will be copied to your clipboard, ready to paste in the implementation.
It’s a pretty simple task: grab all of the properties from a class header and figure out an NSString format placeholder for each type. This script uses a pretty basic lookup table and can’t do much with custom types, so you’ll just get %@
for anything unrecognized. When you paste the result into your .m
file, let Xcode warn you about any mismatched placeholders and clean up manually.
As an example, if I run it on this class from Gus Mueller, I get this method in my clipboard:
- (NSString *)description {
return [NSString stringWithFormat:@"FMDatabase description:
%@\ntraceExecution: %i\ncrashOnErrors: %i\ncachedStatements:
%@\ngoodConnection: %i\nchanges: %zd\nhasOpenResultSets:
%i\ndatabasePath: %@\nsqliteHandle: %@\nuseCount: %@\nstatement: %@",
[super description], self.traceExecution, self.crashOnErrors,
self.cachedStatements, self.goodConnection, self.changes,
self.hasOpenResultSets, self.databasePath, self.sqliteHandle,
self.useCount, self.statement];
}
If you try this out and have any suggestions, let me know. I’ve only tested this on a few classes at this point, so I’m probably missing a few things.
Xcode Describe Service v1.0.0
Download Xcode Describe Service v1.0.0
Create description methods from header files for Objective-C classes
Published 03/14/21.
Updated 03/14/21. Changelog
I also created a TextBuddy script for this. I’m growing very fond of that app. I’ll be collecting my scripts in this TextBuddyScripts repository on GitHub. The README there explains how to use them. This is my first for-public-consumption TextBuddy script, so the repo is a little thin right now, but it may grow over time1.
-
Assuming Tyler doesn’t keep implementing my crazy requests himself. ↩