Does Xcode support regions?

2019-01-14 19:53发布

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

7条回答
ら.Afraid
2楼-- · 2019-01-14 20:35

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-14 20:37

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.

查看更多
Juvenile、少年°
4楼-- · 2019-01-14 20:47

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
查看更多
祖国的老花朵
5楼-- · 2019-01-14 20:49

Try this way :

//region title1
{
    //region Subtitl1
    {

    }
    //region Subtitl2
    {

    }
}

It can do like that :

It can do like that :

查看更多
beautiful°
6楼-- · 2019-01-14 20:52

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.

查看更多
太酷不给撩
7楼-- · 2019-01-14 20:55

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.

查看更多
登录 后发表回答