When writing c#, in Visual Studio, you can set regions to group code accordingly. Being new to Xcode I can't seem to find a similar feature.
-
Don't do this, hidden code, out of sight, out of mind, is dangerous. IMHO. First thing I do in any editor is turn off code folding. – Mark Allanson Aug 28 '11 at 15:00
-
2@MarkAllanson, what, like in dlls? :) I disagree, often you want to work on a code file and ignore large parts of it. Such as working with the delegate methods of a tableview, when your data source is done and dusted, or collapsing large parts of a utility class, so you can work on the current method. – Dermot Mar 07 '13 at 23:18
6 Answers
Using Xcode editor, you can collapse or expand any block of code by clicking in the little left margin. Moreover, you can put a mark in any point in the code with:
#pragma mark
your title as long as you want
Your mark will then appear in the middle popup menu on top of the editor window.
Update: I have found that a duplicate of this question exists here. The answers may be of interest.
-
1That is similar, but not quite the same. Regions have start and end points you can also collapse and expand. – Ferdeen Feb 22 '09 at 15:26
-
the #pragma mark doesn't seem to do the same thing. But it's better than nothing. Thanks. – Ferdeen Feb 22 '09 at 15:27
-
Yes, since Xcode does not implement #region, I've tried to find something similar. – mouviciel Feb 22 '09 at 15:37
You can also use:
// MARK: -
or
// MARK: Initialization
which is more portable than #pragma mark.

- 25,219
- 10
- 103
- 255
-
1this one is actually cool but gets lost in comments as they both have same green colour. but albeit nice – codejunkie Aug 11 '12 at 05:49
Sorry for being pedantic but it's "Xcode", not "X-Code".
You can for example go:
#pragma mark -
or
#pragma mark Initialization
This will give you this kind of thing:

- 21,988
- 13
- 81
- 109

- 46,656
- 50
- 121
- 144
-
1no need to be pragmatic I need to know what's right. Thanks and thanks for the screen shot. – Ferdeen Feb 22 '09 at 19:19
If you want to have a folding zone with a custom section of your code then put your desired code inside braces {
}
, and it will become a folding zone.
But you have to keep in mind that braces also define variables scope, so this code should not have variables declarations which will be used outside these brackets.

- 97,681
- 90
- 411
- 885

- 2,947
- 28
- 35
-
And is also syntactically legal only where a block is legal. You can't, for example, group a set of function declarations with this. – JeremyP Mar 31 '13 at 08:06
Without support for .Net style regions, being able to collapse all your functions is the next best thing.
command-option-shift-left arrow to collapse.
command-option-shift-right arrow to expand.
Xcode will remember the last state of collapsed functions.

- 11,857
- 5
- 62
- 68