Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fold blocks of comments in Swift using Xcode the way I do it in Visual Studio?

Tags:

xcode

swift

Edit: It seems to be a bug which only occurs while using Xcode in combination with Swift. In Objective-C it seems to work fine. I sent out a bug report and will post the news from apple.

I started programming Swift 2 in Xcode after some time programming C++ in Visual Studio.

But I can't find a proper way to get the comments the way I did them in Visual Studio.

Sometimes I just want to comment things out and don't want to see them again until I decide otherwise so I have free space to think about a new approach to a problem. In this way I'm not distracted and find a better solution much easier. And if I don't find a more suitable solution I can still the use the "backup" within seconds.

How can I achieve this - which I have done in Visual Studio - in Xcode6 and Xcode7 Beta?

Here is a Screenshot which illustrates commenting in Visual Studio 2015:

folded

I just want these tiny snippets which use one line of code and can easily be opened and uncommented.

I haven't found a solution for this problem anywhere. I only found some methods which seemed to work in an older version of Xcode but not anymore. Please be so kind and check them in your latest version of Xcode before you post them because I already got some methods which are apparently not longer working.

like image 775
remedy Avatar asked Sep 14 '15 20:09

remedy


People also ask

How do you collapse code blocks in Xcode?

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.


2 Answers

The folding of comments in both Swift and Objective-C works in Xcode 9, by clicking in the comment and then pressing command+option+.

enter image description here

For earlier Xcode versions, see my original answer below.


In Xcode versions before Xcode 9, comment folding didn't work for Swift, but rather only worked for Objective-C. (And the original question didn't specify the language.) And for it to work with Objective-C in Xcode 8 and earlier, you needed to turn on the code folding option under "Text Editing" in "Preferences":

enter image description here

(In Xcode 9, code folding is always enabled and this option does not appear in Preferences.)

Then you can collapse the Objective-C comments by pressing command+option+ (or in Xcode versions prior to 9, you can click in the folding area in the left margin):

enter image description here

like image 75
Rob Avatar answered Sep 18 '22 07:09

Rob


Currently (as of Xcode 7.2), you cannot fold Swift source files.

To fold Objective-C comments, you need to use a style of comment that has a begin and end like this:

/* Start comment
End comment */

Then, on the line where the comment begins, hover your cursor over the tiny column next to the line number, and click the arrow to fold it. Click the arrow again to unfold it.

like image 43
2 revs Avatar answered Sep 19 '22 07:09

2 revs