24

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.

Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
Ferdeen
  • 21,332
  • 6
  • 29
  • 31
  • 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 Answers6

22

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.

Community
  • 1
  • 1
mouviciel
  • 66,855
  • 13
  • 106
  • 140
22

You can also use:

// MARK: -

or

// MARK: Initialization

which is more portable than #pragma mark.

0xced
  • 25,219
  • 10
  • 103
  • 255
11

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:

alt text

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
7

one more option

#pragma mark - Initialization
bturner
  • 514
  • 3
  • 9
3

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Kibernetik
  • 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
3

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.

Carter Medlin
  • 11,857
  • 5
  • 62
  • 68