Xcode Swift 代码折叠/折叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40285997/
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-15 09:30:22  来源:igfitidea点击:

Xcode Swift code folding/collapse

iosswiftxcodemacoscode-folding

提问by brianLikeApple

I know we can use "Shift + Alt + Cmd + Left" to fold/collapse swift code in Xcode, but can anyone tell me how to fold/collapse for functions only? Not properties? Or any alternative way?

我知道我们可以使用“Shift + Alt + Cmd + Left”在 Xcode 中折叠/折叠 swift 代码,但谁能告诉我如何折叠/折叠功能不是属性或任何替代方式

The default folding method does the job, but actually also hide very important information from us......

默认的折叠方法完成了这项工作,但实际上也对我们隐藏了非常重要的信息......

enter image description here

在此处输入图片说明

This is what I wanted!

这就是我想要的!

enter image description here

在此处输入图片说明

回答by Krunal

Updates in Xcode 10:

Xcode 10 中的更新:

Xcode 10 has increased support for code folding, including:

Xcode 10 增加了对代码折叠的支持,包括:

  1. A new code folding ribbon showing all of the multi-line foldable blocks of code in the editor
  2. A new style for folded code in the editor that allows you to edit lines with folded code
  3. Support for folding any block of code enclosed in curly braces
  4. Support for folding blocks of code from the folding ribbon, from structured selection, or from the
  1. 显示编辑器中所有多行可折叠代码块的新代码折叠功能区
  2. 编辑器中折叠代码的新样式,允许您编辑带有折叠代码的行
  3. 支持折叠花括号中的任何代码块
  4. 支持从折叠功能区、结构化选择或从

Menubar ? Editor ? Code Folding ? Fold menu item

菜单栏 ?编辑?代码折叠 ? 折叠菜单项

enter image description here

在此处输入图片说明

Look at this snapshot:

看看这个快照:

enter image description here

在此处输入图片说明



Code folding was disabled in Xcode 9 beta 1, which is working now, in Xcode 9 Beta5 according to beta release note: Resolved in Xcode 9 beta 5 – IDE

代码折叠在 Xcode 9 beta 1 中被禁用,它现在正在运行,在 Xcode 9 Beta5 中根据 beta 版本说明:在 Xcode 9 beta 5 – IDE 中解决

Here is how:

方法如下:

  1. Press and hold ?(command) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
  2. Keep (hold) ?(command) button in pressed condition and click on highlighted area. It will enable quick menu popover window with Foldoption.
  3. Select Foldfrom menu list. It will fold your code and shows 3 dots, folding/covering entire block.
  4. Now, to again unfold your code block, release ?(command) button and click on 3 dots folding a block.
  1. 按住?键盘上的(命令)按钮并将鼠标光标移动/悬停在任何(开始或结束)大括号上。它会自动突出显示,阻止区域。
  2. 保持(按住)?(命令)按钮处于按下状态,然后单击突出显示的区域。它将启用带有Fold选项的快速菜单弹出窗口。
  3. Fold从菜单列表中选择。它将折叠您的代码并显示 3 个点,折叠/覆盖整个块。
  4. 现在,再次展开您的代码块,释放?(命令)按钮并单击折叠块的 3 个点。

For easy understanding, look at this snapshot:

为了便于理解,请看这张快照:

enter image description here

在此处输入图片说明


It's all keyboard short cuts are also working.


所有的键盘快捷键也都有效。

Fold                          ? ? ←      option + command + left arrow
Unfold                        ? ? →      option + command + right arrow
Unfold All                    ? U        option + U
Fold Methods & Functions      ? ? ↑      option + command + up arrow
Unfold Methods & Functions    ? ? ↓      option + command + down arrow
Fold Comment Blocks           ? ? ? ↑    control + shift + command + up
Unfold Comment Blocks         ? ? ? ↓    control + shift + command + down
Focus Follows Selection       ? ? ? F    control + option + command + F
Fold All                      ? ? ? ←    command + option + shift + left
Unfold All                    ? ? ? →    command + option + shift + left


Code folding options from Xcode Menu:


Xcode 菜单中的代码折叠选项

Menubar ? Editor ? Code Folding ? "Here is list of code folding options"

菜单栏 ?编辑?代码折叠 ? “这是代码折叠选项列表”

Here is ref snapshot:

这是参考快照:

enter image description here

在此处输入图片说明

Same options from Xcode Short-cut list:

来自 Xcode Short-cut list 的相同选项

Menubar ? Xcode ? Preferences ? Key Bindings ? "Here is list of code folding short-keys"

菜单栏 ?代码?喜好 ?键绑定?“这是代码折叠快捷键列表”

enter image description here

在此处输入图片说明

回答by Karthick Selvaraj

As of now there is no optionto fold only functions in you class, we may expect these features in future release of Xcode. To fold specific function you can use alt + command + left_arrowafter placing the cursor in function starting line. Use alt + command + right_arrowfor unfold folded function.

到目前为止,还没有选项仅折叠您类中的函数,我们可能希望在未来版本的Xcode. 要折叠特定功能,您可以alt + command + left_arrow将光标放在功能起始行后使用。使用alt + command + right_arrow的展开折叠功能。

回答by Ajil O.

Another way to do this would be as below.

另一种方法如下。

  • Go to Xcode preferences (command + ,)
  • Select the text editing tab
  • Check code folding ribbon
  • 转到 Xcode 首选项(命令 + ,)
  • 选择文本编辑选项卡
  • 校验码折叠色带

enter image description here

在此处输入图片说明

Now when you look at your code you should be able to see this :

现在,当您查看代码时,您应该能够看到:

enter image description here

在此处输入图片说明

When you bring your mouse over the folding ribbon you should see a small arrow that can be used to fold code blocks (functions, if-else, loops, etc)

当您将鼠标移到折叠功能区上时,您应该会看到一个小箭头,可用于折叠代码块(函数、if-else、循环等)

Hope this helps!

希望这可以帮助!

回答by MiladiuM

you can always go to Symbol Navigator, cmd+2and enjoy list of all your declarations that swift has organized. However, if this doesn't satisfy you, and you require to have better control over your functions I recommend you to use function protocols, they work somewhat similar to header files in C. you can find out more about function prototypes in(https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID267)

您可以随时访问Symbol Navigatorcmd+2并享受 swift 组织的所有声明列表。但是,如果这不能满足您的要求,并且您需要更好地控制您的函数,我建议您使用function protocols,它们的工作方式与C. 您可以在(https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID267)中找到有关函数原型的更多信息