Can you set code regions in Xcode?

2020-02-26 04:45发布

问题:

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.

回答1:

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.



回答2:

You can also use:

// MARK: -

or

// MARK: Initialization

which is more portable than #pragma mark.



回答3:

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:



回答4:

one more option

#pragma mark - Initialization


回答5:

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.



回答6:

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.