Does Xcode support regions?

后端 未结 8 1346
慢半拍i
慢半拍i 2020-12-06 04:15

Does Xcode support anything akin to Visual Studio style #region directives for arbitrary code folding?

相关标签:
8条回答
  • 2020-12-06 04:26

    That won't work in the place you want it most, that is, around groups of functions or methods.

    It may be useful inside a long, linear method with no internal conditionals or loops, but such methods aren't common in general Mac OS X UI code, though if you're writing some big numeric or graphics-crunching code it could help group things.

    And the if(fold) is entirely superfluous. Just use the braces inside a method or function and Xcode will fold them.

    0 讨论(0)
  • 2020-12-06 04:26

    Without support for .Net style regions, being able to collapse all your functions at the same time is the next best thing.

    command-option-shift-left arrow to collapse all.

    command-option-shift-right arrow to expand all.

    Xcode will remember the last state of collapsed functions.

    0 讨论(0)
  • 2020-12-06 04:30

    No, you can only fold code on various defined scoping levels in Xcode.

    You can use little tricks to make navigating via the function menu easier, though.

    #pragma mark
    

    Allows you to create a grouping where the label following mark will show up in the function menu. If the label is a hyphen, a separator is inserted into the function menu.

    Also, the following labels in comments will show up in the function menu:

    // MARK:
    // TODO:
    // FIXME:
    // !!!:
    // ???:
    

    Obviously since #pragma mark is not really portable, if you're building a portable application and need it to work with a compiler that doesn't just ignore #pragma directives that it doesn't understand, the comment-style mark is a decent alternative.

    0 讨论(0)
  • 2020-12-06 04:32

    One nice solution I just found:

    Put your project into one big namespace. Close and reopen this namespace for the individual sections of your source file:

    namespace myproj { // members of class MyClassA
    
    void MyClassA::dosomething()
    {
    }
    
    void MyClassA::dosomethingelse()
    {
    }
    
    } // members of class MyClassA
    namespace myproj { // members of MyClassB
    
    void MyClassB::dosomething()
    {
    }
    
    void MyClassB::dosomethingelse()
    {
    }
    
    } // members of MyClassB
    
    0 讨论(0)
  • Put your desired code inside brackets { }, and it will become a folding zone.

    But you have to keep in mind that brackets also define variables scope, so this code should not have variables declarations which will be used outside these brackets.

    0 讨论(0)
  • 2020-12-06 04:35

    Try this way :

    //region title1
    {
        //region Subtitl1
        {
    
        }
        //region Subtitl2
        {
    
        }
    }
    

    It can do like that :

    It can do like that :

    0 讨论(0)
提交回复
热议问题