41

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
iano
  • 2,061
  • 1
  • 18
  • 22

9 Answers9

57

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.

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
  • 2
    The C standard says a conforming implementation *must* ignore `#pragma` directives that it doesn't understand. Of course, every `#pragma` directive that does not start with `STDC` is implementation-defined anyway, so still not that portable. – dreamlax Dec 13 '11 at 18:59
  • Somebody should definitely make an XCode plugin for custom code folding. That would be very useful :) – brimstone Nov 26 '15 at 21:03
15

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.

willc2
  • 38,991
  • 25
  • 88
  • 99
  • 9
    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. – Chuck Apr 04 '09 at 08:21
  • 1
    Another +1 for most IDEs not supporting preprocessing directive for code folding, -1 for i think this is because they want to force us for method usage rather than folding, +1 for this would be awesome for method grouping, -1 for i think this is because they want to force us to use classes. So we are even :P. Excuse my ignorance, if there exist any. – Gökhan Barış Aker Sep 08 '12 at 11:47
  • 1
    Why bother with the define? Why not just if (true)? – ftvs Nov 26 '13 at 05:09
  • 1
    @ftvs I imagine because he doesn't want to go through changing all the trues when he wants to actually read his code again – Bradley Thomas Feb 02 '14 at 19:16
4

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.

cdespinosa
  • 20,661
  • 6
  • 33
  • 39
2

Try this way :

//region title1
{
    //region Subtitl1
    {

    }
    //region Subtitl2
    {

    }
}

It can do like that :

It can do like that :

1

Without support for .Net style regions, being able to collapse all your functions at the same time is the next best thing.

command-option-shift-left arrow to collapse all.

command-option-shift-right arrow to expand all.

Xcode will remember the last state of collapsed functions.

Carter Medlin
  • 11,857
  • 5
  • 62
  • 68
  • NB: in Xcode 8, to make Xcode remember your folding, ensure your project format is set to `Xcode 8.0-compatible`. – Cemen Jul 18 '17 at 09:38
1

A useful option in XCode 12 (maybe before), is an option in preferences "Code Folding Ribbon"

Code folding ribbon

When you check it, the source code looks like this

Ribbon in code

When you hover the mouse over this ribbon, you get foldable regions based on brackets, like this

Foldable ribbon

When you click the Ribbon, it folds the bracket region, like this

Folded ribbon

Its not as the regions in Visual Studio, where you can place them wherever you want, but they're good enough to tidy up your code files.

Yaman
  • 1,030
  • 17
  • 33
1

To answer your question...No. And It drives me nuts.

If you have the opportunity/ability you can use AppCode for this. I've been using it for a few years and it usually beats Xcode in many areas.

Also I specifically use AppCode because of these features:

  • Ability to use regions
  • Searching classes, text and usages is MUCH faster.
  • Refactoring is also faster.
  • Cleaner and more customizable UI.
  • Tabs are handled (in my opinion) much better than in Xcode.
  • FOLDING. You can actually change what levels of folding you want. Why Apple thought there should be no quick-key to fold extensions is beyond me. And fold ribbons? Really Apple? Yes they're pretty and all but most professionals use hotkeys for everything.
  • Better GIT integration.
  • Support for live updates in SwiftUI
  • If you use other Jetbrains IDE's like PyCharm or Android Studio the UI is exactly the same.

Some downsides of AppCode:

  • Some things that work in Xcode aren't supported
    • Visual #colorLiteral(). When using them they don't show a color picker.
    • No Storyboard support. Annoying to have to open up Xcode. If you write your UI in code this is a moot point.
  • Editing .plist files isn't as nice. Doable, but not nice.
  • Initial indexing can take a while.
  • Cost. But I would argue the time savings in just navigation will compensate for this.

Kind of a lot for a simple question but I think it's nice having alternatives.

Rob Barber
  • 474
  • 3
  • 10
0

Put your desired code inside brackets { }, and it will become a folding zone.

But you have to keep in mind that brackets also define variables scope, so this code should not have variables declarations which will be used outside these brackets.

Kibernetik
  • 2,947
  • 28
  • 35
-3

One nice solution I just found:

Put your project into one big namespace. Close and reopen this namespace for the individual sections of your source file:

namespace myproj { // members of class MyClassA

void MyClassA::dosomething()
{
}

void MyClassA::dosomethingelse()
{
}

} // members of class MyClassA
namespace myproj { // members of MyClassB

void MyClassB::dosomething()
{
}

void MyClassB::dosomethingelse()
{
}

} // members of MyClassB