What is the significance of #pragma marks? Why do we need #pragma marks?

前端 未结 7 1380
耶瑟儿~
耶瑟儿~ 2021-01-29 17:21

What is the purpose of #pragma marks in Xcode? Does their location in .m files matter? Should some #pragma come before all others?

相关标签:
7条回答
  • 2021-01-29 17:56

    In simple word we can say that #pragma mark - is used for categorizing methods, so you can find your methods easily. It is very useful for long projects.

    0 讨论(0)
  • 2021-01-29 17:56
    #pragma mark - NSSecureCoding
    

    The main purpose of "pragma" is for developer reference.

    You can easily find a method/Function in a vast thousands of coding lines.

    Xcode 11+:

    Marker Line in Top

    // MARK: - Properties
    

    Marker Line in Top and Bottom

    // MARK: - Properties - 
    

    Marker Line only in bottom

    // MARK: Properties -
    
    0 讨论(0)
  • 2021-01-29 17:58

    Just to add the information I was looking for: pragma mark is Xcode specific, so if you deal with a C++ project that you open in different IDEs, it does not have any effect there. In Qt Creator, for example, it does not add categories for methods, nor generate any warnings/errors.

    EDIT

    #pragma is a preprocessor directive which comes from C programming language. Its purpose is to specify implementation-dependent information to the compiler - that is, each compiler might choose to interpret this directive as it wants. That said, it is rather considered an extension which does not change/influence the code itself. So compilers might as well ignore it.

    Xcode is an IDE which takes advantage of #pragma and uses it in its own specific way. The point is, #pragma is not Xcode and even Objective-C specific.

    0 讨论(0)
  • 2021-01-29 17:59

    #pragma mark is used to tag the group of methods so you may easily find and detect methods from the Jump Bar. It may help you when your code files reach about 1000 lines and you want to find methods quickly through the category from Jump box.

    In a long program it becomes difficult to remember and find a method name. So pragma mark allows you to categorize methods according to the work they do. For example, you tagged some tag for Table View Protocol Methods, AlertView Methods, Init Methods, Declaration etc.

    #pragma mark is the facility for XCode but it has no impact on your code. It merely helps to make it easier to find methods while coding.

    0 讨论(0)
  • 2021-01-29 18:01

    While searching for doc to point to about how pragma are directives for the compiler, I found this NSHipster article that does the job pretty well.

    I hope you'll enjoy the reading

    0 讨论(0)
  • 2021-01-29 18:13

    #pragma mark directives show up in Xcode in the menus for direct access to methods. They have no impact on the program at all.

    For example, using it with Xcode 4 will make those items appear directly in the Jump Bar.

    There is a special pragma mark - which creates a line.

    enter image description here

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