Thursday, April 28, 2011

Xcode regions

Does Xcode support anything akin to Visual Studio style #region directives for arbitrary code folding?

From stackoverflow
  • No, you can only fold code on various defined scoping levels in Xcode.

    You can use little tricks to make navigating via the function menu easier, though.

    #pragma mark
    

    Allows you to create a grouping where the label following mark will show up in the function menu. If the label is a hyphen, a separator is inserted into the function menu.

    Also, the following labels in comments will show up in the function menu:

    // MARK:
    // TODO:
    // FIXME:
    // !!!:
    // ???:
    

    Obviously since #pragma mark is not really portable, if you're building a portable application and need it to work with a compiler that doesn't just ignore #pragma directives that it doesn't understand, the comment-style mark is a decent alternative.

  • I am going to hell for this but here goes:

    At the top of a given file, put

    #define FOLD 1
    

    Wherever you want to fold something, wrap it in an if block like so:

    if(FOLD) {
     // your code to hide
     // more code
    }
    

    That will let you fold it away out of sight.

    Chuck : It would be a good idea, but you can only put an if statement inside a function scope. You can't wrap struct, enum and function definitions in an if.
  • That won't work in the place you want it most, that is, around groups of functions or methods.

    It may be useful inside a long, linear method with no internal conditionals or loops, but such methods aren't common in general Mac OS X UI code, though if you're writing some big numeric or graphics-crunching code it could help group things.

    And the if(fold) is entirely superfluous. Just use the braces inside a method or function and Xcode will fold them.

0 comments:

Post a Comment