What does the NS prefix mean?

前端 未结 9 998
别跟我提以往
别跟我提以往 2020-12-02 03:32

Many classes in Cocoa/Cocoa Touch have the NS prefix. What does it mean?

相关标签:
9条回答
  • 2020-12-02 04:17

    When NeXT were defining the NextStep API (as opposed to the NEXTSTEP operating system), they used the prefix NX, as in NXConstantString. When they were writing the OpenStep specification with Sun (not to be confused with the OPENSTEP operating system) they used the NS prefix, as in NSObject.

    0 讨论(0)
  • 2020-12-02 04:21

    Basically NS comes from NextSTEP, the original operating system that became Mac OS X when Apple acquired Next.

    I want to explain something else and this is why exactly it's needed.

    In C++ there are namespaces and almost anything goes in std

    This is why you have std::string.

    Namespaces are used so it's harder for you to make a mistake and you can write your own class string without conflicting with the system one.

    Objective-C is superset of C, but it doesn't include namespaces and for the same reason above all system classes are prefixed with NS or some other strange prefix.

    This thing is the same of how all DirectX classes are prefixed with D3D and how all OpenGL classes are prefixed with gl.

    This means that you should not use NS to name your own classes and when you see NS, CA in Core Animation or CG in Core Graphics you understand that this is a call to a system framework.

    Swift changes this convention, because Swift supports namespacing and it maps its core types like String to the NS equivalents.

    0 讨论(0)
  • 2020-12-02 04:23

    It's from the NeXTSTEP heritage.

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