660

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

1
  • 3
    I prefer to believe it means "namespace" 😂
    – Andy
    Commented Sep 25, 2020 at 18:47

9 Answers 9

848

The original code for the Cocoa frameworks came from the NeXTSTEP libraries Foundation and AppKit (those names are still used by Apple's Cocoa frameworks), and the NextStep engineers chose to prefix their symbols with NS.

Because Objective-C is an extension of C and thus doesn't have namespaces like in C++, symbols must be prefixed with a unique prefix so that they don't collide. This is particularly important for symbols defined in a framework.

If you are writing an application, such that your code is only likely ever to use your symbols, you don't have to worry about this. But if you're writing a framework or library for others' use, you should also prefix your symbols with a unique prefix. CocoaDev has a page where many developers in the Cocoa community have listed their "chosen" prefixes. You may also find this SO discussion helpful.

0
508

It's from the NeXTSTEP heritage.

3
  • 9
    ...or perhaps the NeXT/Sun heritage (Sun was a participant in the OpenStep consortium)
    – Barry Wark
    Commented Aug 8, 2012 at 15:33
  • 30
    Yes, of course. It's all beautifully laid out in the other answers, that's why I didn't bother copying that information into mine. Sorry if that offends someone - I don't mind if the accepted answer changes to one of the others (if that's possible - never checked that).
    – Olaf Kock
    Commented Aug 8, 2012 at 20:21
  • 3
    I would like to see more but the link to NeXTSTEP Commented Oct 22, 2015 at 21:16
160

NeXTSTEP or NeXTSTEP/Sun depending on who you are asking.

Sun had a fairly large investment in OpenStep for a while. Before Sun entered the picture most things in the foundation, even though it wasn't known as the foundation back then, was prefixed NX, for NeXT, and sometime just before Sun entered the picture everything was renamed to NS. The S most likely did not stand for Sun then but after Sun stepped in the general consensus was that it stood for Sun to honor their involvement.

I actually had a reference for this but I can't find it right now. I will update the post if/when I find it again.

4
63

It is the NextStep (= NS) heritage. NeXT was the computer company that Steve Jobs formed after he quit Apple in 1985, and NextStep was it's operating system (UNIX based) together with the Obj-C language and runtime. Together with it's libraries and tools, NextStep was later renamed OpenStep (which was also the name on an API that NeXT developed together with Sun), which in turn later became Cocoa.

These different names are actually quite confusing (especially since some of the names differs only in which characters are upper or lower case..), try this for an explanation:

TheMerger OpenstepConfusion

2
  • 5
    I could have sworn Apple fired him for various reasons.
    – anon
    Commented Jul 12, 2016 at 22:46
  • @Nic Jobs quit in Sept '85 after losing the power struggle with Sculley.
    – dlchambers
    Commented Jan 25, 2022 at 14:38
51

From Apple's developer docs:

Historical Note: If you’re wondering why so many of the classes you encounter have an NS prefix, it’s because of the past history of Cocoa and Cocoa Touch. Cocoa began life as the collected frameworks used to build apps for the NeXTStep operating system. When Apple purchased NeXT back in 1996, much of NeXTStep was incorporated into OS X, including the existing class names. Cocoa Touch was introduced as the iOS equivalent of Cocoa; some classes are available in both Cocoa and Cocoa Touch, though there are also a large number of classes unique to each platform. Two-letter prefixes like NS and UI (for User Interface elements on iOS) are reserved for use by Apple.

Source: Programming with Objective-C

32

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.

25

From Cocoa_(API) Wikipedia:

(emphasis added)

Cocoa classes begin with the acronym "NS" (standing either for the NeXT-Sun creation of OpenStep, or for the original proprietary term for the OpenStep framework, NeXTSTEP): NSString, NSArray, etc.

Foundation Kit, or more commonly simply Foundation, first appeared in OpenStep. On Mac OS X, it is based on Core Foundation. Foundation is a generic object-oriented library providing string and value manipulation, containers and iteration, distributed computing, run loops, and other functions that are not directly tied to the graphical user interface. The "NS" prefix, used for all classes and constants in the framework, comes from Cocoa's OPENSTEP heritage, which was jointly developed by NeXT and Sun.

2
  • Wikipedia is wrong here. Foundation Kit first appeared in Enterprise Objects Framework, which predates OpenStep. Incidentally the NeXT version of Foundation was supposedly based on a CoreFoundation-like C API, but that wasn't exposed as public interface until Mac OS X.
    – user23743
    Commented May 3, 2013 at 10:55
  • @user23743, I don't think you're correct. EOF predates the OPENSTEP operating system, but not the OpenStep API. The EOF documentation you linked to directly (references OpenStep)[cilinder.be/docs/next/NeXTStep/3.3/nd/Foundation/…, and they were both released in 1994.
    – AriX
    Commented Apr 25, 2014 at 21:43
12

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.

8

Bill Bumgarner aka @bbum, who should know, posted on the CocoaBuilder mailing list in 2005:

Sun entered the picture a bit after the NS prefix had come into play. The NS prefix came about in public APIs during the move from NeXTSTEP 3.0 to NeXTSTEP 4.0 (also known as OpenStep). Prior to 4.0, a handful of symbols used the NX prefix, but most classes provided by the system libraries were not prefixed at all -- List, Hashtable, View, etc...

It seems that everyone agrees that the prefix NX (for NeXT) was used until 1993/1994, and Apple's docs say:

The official OpenStep API, published in September of 1994, was the first to split the API between Foundation and Application Kit and the first to use the “NS” prefix.

Not the answer you're looking for? Browse other questions tagged or ask your own question.