Xcode Swift code folding/collapse - ios

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......
This is what I wanted!

Updates in Xcode 10:
Xcode 10 has increased support for code folding, including:
A new code folding ribbon showing all of the multi-line foldable blocks of code in the editor
A new style for folded code in the editor that allows you to edit lines with folded code
Support for folding any block of code enclosed in curly braces
Support for folding blocks of code from the folding ribbon, from structured selection, or from the
Menubar ► Editor ► Code Folding ► Fold menu item
Look at this snapshot:
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:
Press and hold ⌘ (command) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
Keep (hold) ⌘ (command) button in pressed condition and click on highlighted area. It will enable quick menu popover window with Fold option.
Select Fold from menu list. It will fold your code and shows 3 dots, folding/covering entire block.
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:
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:
Same options from Xcode Short-cut list:
Menubar ► Xcode ► Preferences ► Key Bindings ► "Here is list of code folding short-keys"

Another way to do this would be as below.
Go to Xcode preferences (command + ,)
Select the text editing tab
Check code folding ribbon
Now when you look at your code you should be able to see this :
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!

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.

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:
and you can fold nested views as deep as you like, with animation:

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)

Related

Code folding not working in Xcode 9.0 beta 2

I use code folding frequently in Xcode. Recently I tried code folding in Xcode 9 beta with shortcut Command + alt + left arrow and nothing happened.
So I tried to do the same with editor option in menu bar but it seems like code folding option is disabled. How can enable code folding option in Xcode 9 and make it to work again like in Xcode 8 .
Code folding is working now, in Xcode 9 Beta5 according to beta release note: Resolved in Xcode 9 beta 5 – IDE
Here is how:
Press and hold ctrl (control) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
Keep (hold) ctrl (control) button in pressed condition and click on highlighted area. It will enable quick menu popover window with Fold option.
Select Fold from menu list. It will fold your code and shows 3 dots, folding/covering entire block.
Now, to again unfold your code block, release ctrl (control) button and click on 3 dots folding a block.
For easy understanding, look at this snapshot:
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
Here is ref snapshot:
See the release notes. Code folding is not yet supported in beta 3:
The source editor in this beta version of Xcode 9.0 doesn’t support code folding. (29396003).
According to a WWDC presentation, they've done a fairly major rewrite of the editor, so don't be surprised if it takes them a while to nail all this down.
And as with all beta software, I'd suggest reviewing the release notes carefully, as there are lots of open issues that are well documented there.
Code folding now works in beta 5.
In Xcode 9 build 9A235 the Code Folding does not always open the methods when clicking on the 3 dots. When this occurs you can click the method below the one you wish to open and this sometimes works. Apple should be embarassed for releasing such a half-baked feature. What's more, the boxes around the folded methods add no value and are simply additional, unnecessary, clutter.

Visual Studio for Mac how to collapse regions and methods

I couldn't find a way to collapse/expand regions and sections from Visual Studio for Mac.
If someone could share how to do that, it would be very useful.
You need to go to Preferences >> Text Editor >> General as on the image below and you will see Code Folding area.
You can check whatever you need and you will see code folding blocks in your code.
menu > help>
type "folding" ,
then move the mouse over list items.
Yes.. You can follow this key patterns to collapse and expand all the methods.
Shift + Option + Command + ArrowDown
Shift + ⌥ + ⌘ + ⬇
OR Follow the Image Screenshot:

iOS - Shortcut for jumping to definition in Xcode 9?

In previous Xcode version , I could jump to definition with simple
Cmd + click on that method/variable .
But in Xcode 9, I feel uncomfortable to jump to definition .
Does anyone has a better solution for jumping to definition in Xcode 9 ?
I am tired of selecting options from dropdown list.
Ashish and Ghulam's answers were great but it still kinda bugged me that things had changed and I couldn't jump to definition as before. Then I found this...
Xcode9Beta2-Preferences->Navigation->Command-click on Code:->Jump To Definition:
Solution 1:
Go to Xcode menu
Click on Preferences
Select Navigation Tab from Top
Select Command-click on Code
Change to "Jumps to Definition"
Solution 2:
Use
Ctrl + ⌘ + Left click
There is short cut displayed on drop down menu, just use-
1. Control, Command and left mouse button
OR
2. Command plus Right Mouse Click
instead of command left mouse button.
In Xcode 9 Beta, you can go definition by Cmd + Right Click
Standard hot key for jump to definition is ctrl+cmd+j. Set cursor to the class/method you are interested in and press this buttons to switch to declaration. Also you can try to press ctrl+opt+cmd+j. In this case definition will be opened in assistant editor
If dont like to use mouse click(I certainly don't like) you could use
Command + Ctrl + J
In Xcode 9 both of these work:
⌘ + Right Click
OR
⌘⌃ + Click
When I ⌘-click on a symbol in Xcode 9 I see
That means you have to ⌃⌘-click on the symbol to skip the popup.
Nevertheless there is even a keyboard shortcut:
I don't know how Cmd + Option + Left Click worked for you guys, but the shortcut (at least for me) was Cmd + Ctrl + Left Click.
I've tried on both Apple keyboard and MacBook keyboard and this is the one that did it.
In Xcode 9 Beta, it has been changed to Cmd + Ctrl + Left Click.
Solution to your question: Ctrl + ⌘ + Left click
Xcode >> Preference >> Key Bindings >> Here is list of all short cuts
of Xcode.
Deleting everything under the Derived data and re-opening Xcode fixed everything for me.

Enabling Code Folding Bar in Xcode

How to enable the code folding bar next to the line numbers?
I googled "code folding Mac"; it's all about triggering code folding and unfolding, but nothing about enabling the bar.
I have tried using Editor -> Code Folding -> Fold/Unfold to fold and unfold codes, but I don't have the vertical bar there next to the line numbers; so, I have the button to click to fold and unfold.
How can I enable that bar?
.
I always encounter this that suddenly I accidentally hide the Code folding ribon. So from #PetahChristian's answer, I captured it with screenshot.
Xcode > Settings > Text Editing > Check "Code Folding Ribbon"
You may also access Settings through shortcut command + ,
Go to Preferences -> Text Editing, and click Show code folding ribbon.
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:
Press and hold ⌘ (command) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
Keep (hold) ⌘ (command) button in pressed condition and click on highlighted area. It will enable quick menu popover window with Fold option.
Select Fold from menu list. It will fold your code and shows 3 dots, folding/covering entire block.
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:
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:
Same options from Xcode Short-cut list:
Menubar ► Xcode ► Preferences ► Key Bindings ► "Here is list of code folding short-keys"
Updated Answer for Xcode 12 and above
Code folding ribbon is quite useful and easy to view code scope.
Go to Preferences -> Text Editing -> Display, and click on Code folding ribbon

What is the way to quick-switch between tabs in Xcode 4

I have opened many tabs while working on project. (new feature in Xcode 4).
But for switching from one tab to other tab, the only way I've found to do this is to use the mouse. Is there any way to switch between different tabs using keyboard shortcuts?
Shortcuts are:
CMD + SHIFT + } - Select Next tab
CMD + SHIFT + { - Select Previous tab
The default shortcuts are:
⌘ + SHIFT + { - Select Next Tab
⌘ + SHIFT + } - Select Previous Tab
But I prefer going to XCode -> Preferences -> Key bindings and changing them to:
CTRL + TAB - Select Next Tab
CTRL + SHIFT + TAB - Select Previous Tab
...so they work the same as in Chrome or Safari.
If the shortcuts don't work (because of language, for example), you can define custom shortcut:
XCode -> Preferences -> Key bindings
Make sure that "all" is selected
Define shortcuts for "Select Previous Tab" and "Select Next Tab"
I used cmd+pageup and cmd+pagedown, didn't generate any conflicts.
XCode Windows menu option shows it as ⌘+ } or ⌘+ { but I think it is a bug since ⌘+ {/} is used for increase indent or decrease indent.
The correct sequence is ⌘+ shift + } or {.
I perfer using BetterTouchTool to switch between tabs(include Chrome, Safari, Finder, Xcode, sublimeText and so on).
BetterTouchTool is a free software.
Just set TipTap Left and TipTap Right to cmd+shift+{ and cmd+shift+}.
And than, you can use trackpads with tiptap to switch between taps.
Here is my configuration:
Hope you will like it.

Resources