Xcode 是否支持区域?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/288508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:25:38  来源:igfitidea点击:

Does Xcode support regions?

objective-cxcodecode-folding

提问by iano

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

Xcode 是否支持任何类似于 Visual Studio 样式#region指令的任意代码折叠?

回答by Jason Coco

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

不,您只能在 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.

显然,由于#pragma 标记并不是真正可移植的,如果您正在构建一个可移植的应用程序并且需要它与一个不只是忽略它不理解的#pragma 指令的编译器一起工作,则注释样式标记是一个不错的选择选择。

回答by willc2

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 块中,如下所示:

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

That will let you fold it away out of sight.

这样你就可以把它折叠起来,看不见了。

回答by cdespinosa

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.

它可能在没有内部条件或循环的长线性方法中很有用,但此类方法在一般 Mac OS X UI 代码中并不常见,但如果您正在编写一些大型数字或图形处理代码,它可以帮助分组事物。

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

而 if(fold) 完全是多余的。只需在方法或函数中使用大括号,Xcode 就会折叠它们。

回答by cdespinosa

Try this way :

试试这个方法:

//region title1
{
    //region Subtitl1
    {

    }
    //region Subtitl2
    {

    }
}

It can do like that :

它可以这样做:

It can do like that :

它可以这样做:

回答by Carter Medlin

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

在不支持 .Net 风格区域的情况下,能够同时折叠所有功能是下一个最好的事情。

command-option-shift-left arrowto collapse all.

command- option- shift-left arrow全部崩溃。

command-option-shift-right arrowto expand all.

command- option- shift-right arrow展开全部。

Xcode will remember the last state of collapsed functions.

Xcode 会记住折叠函数的最后状态。

回答by Kibernetik

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.

但是你必须记住,括号也定义了变量范围,所以这段代码不应该有将在这些括号之外使用的变量声明。

回答by Norbert Nemec

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