aepryus aepryus - 4 months ago 17
Objective-C Question

Automatically convert comment to #pragma mark

I have long liked to visually segment Protocol implementation code using the following:

// UIApplicationDelegate ===========================================================================
- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)options {


However, the #pragma mark feature in Xcode is really quite useful. I also like the ability to add dividing lines. But this means my code now looks like:

// UIApplicationDelegate ===========================================================================
#pragma mark -
#pragma mark UIApplicationDelegate

- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)options {


Which is becoming quite unwieldy. I really like the nice (normally) green line marked with the protocol embedded into the code itself, but I also like the pragma mark utility.

Is there anyway using preprocessor
#define
or other such commands to automatically convert the first into the 2nd?

Answer

Clang / Xcode also recognizes // MARK: for the same purpose as #pragma mark. (Also, this is the only style in Swift, since that language doesn't have a preprocessor.) So if you like green, you could do:

// MARK: - UIApplicationDelegate
// ========================

If you're looking for a way to automatically convert all uses of your previous notation to // MARK or #pragma, I'd recommend a good regex find/replace utility, of which there are too many to list here.