Xcode Swift code folding/collapse
Asked Answered
S

5

45

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?

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

Sexy answered 27/10, 2016 at 13:34 Comment(2)
I searched for it in developer's library. Found nothing :(Kwakiutl
Seems like an Xcode bug to fold a line of code like that doesn't contain braces. Maybe that's just me.Varipapa
S
78

Updates in Xcode 10:

Xcode 10 has increased support for code folding, including:

  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

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

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 Fold option.
  3. Select Fold from 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.

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:

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:

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

enter image description here

Suffuse answered 28/8, 2017 at 13:34 Comment(1)
Great explanation. Thanks for sharing various ways to achieve it.Bette
M
10

As of now there is no option to 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_arrow after placing the cursor in function starting line. Use alt + command + right_arrow for unfold folded function.

Msg answered 1/11, 2016 at 12:49 Comment(0)
B
10

Another way to do this would be as below.

  • Go to Xcode preferences (command + ,)
  • Select the text editing tab
  • Check code folding ribbon

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)

Hope this helps!

Beefwitted answered 2/11, 2016 at 3:46 Comment(2)
Not currently available in Version 9.0 (9A235)Tillandsia
Thanks, @Ajil. This is going to be so helpful.Bette
S
8

Code Ribbons at the rescue for SwiftUI

It is a great feature to have. It seems too few developers know about this. Especially when writing code in SwiftUI, I like to see that my code works before refactoring it into separate views.

Folding is a great tool to have.

Here an example in Xcode 12. It is not active by default but you need too check the box "Code Folding Ribbon" in the preferences:

enter image description here

and you can fold nested views as deep as you like, with animation:

enter image description here

Silvas answered 29/10, 2020 at 16:13 Comment(1)
Thank you so much! This is a must have feature if your developing in SwiftUI!Kaikaia
R
2

you can always go to Symbol Navigator, cmd+2 and 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)

Reseta answered 4/11, 2016 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.