关于iOS:ns前缀是什么意思?

What does the NS prefix mean?

cocoa/cocoa touch中的许多类都有ns前缀。这是什么意思?


COCOA框架的原始代码来自NeXSTEP库基础和AppKT(这些名称仍然被苹果的COCOA框架使用),而下一步工程师选择用NS预置它们的符号。

因为Objto-C是C的扩展,因此没有像C++那样的命名空间,所以符号必须用唯一的前缀前缀,这样它们就不会发生冲突。这对于框架中定义的符号尤其重要。

如果您正在编写一个应用程序,这样您的代码就只可能使用您的符号,那么您不必为此担心。但是,如果您编写的是供他人使用的框架或库,则还应为符号添加唯一前缀。CocaDev有一个页面,在该页面中,Cocoa社区中的许多开发人员都列出了他们所选的前缀。你也会发现这样的讨论很有帮助。


这是下一步的传统。


nextstep或nextstep/sun取决于你问的是谁。

Sun在OpenStep上有一段时间投入了相当大的资金。在太阳进入图片之前,基金会中的大多数东西,即使它不被称为基础,那么,前缀NX,为下一个,并且在太阳进入图片之前的某个时候,一切都被重命名为NS。年代最有可能不代表太阳,但在太阳步入普遍共识后,它代表太阳尊重他们的参与。

我确实有这个的参考资料,但现在找不到。如果我再次找到这篇文章,我会更新它。


这是下一步的遗产。其次是史蒂夫·乔布斯在1985年离开苹果后成立的计算机公司,NextStep是一个基于Unix的操作系统,以及obj-c语言和运行时。与它的库和工具一起,nextstep后来被重新命名为openstep(它也是下一次与sun一起开发的API上的名称),而后又成为cocoa。

这些不同的名称实际上非常令人困惑(特别是由于某些名称仅在字符大小写为大写或小写的情况下有所不同),请尝试以下说明:

目标打开步骤混淆


来自苹果开发者文档:

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.

资料来源:用Objective-C编程


来自可可(API)维基百科:

(增加了强调)

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.


当next定义nextstep api(与nextstep操作系统相反)时,它们使用前缀nx,如nxconstantstring中那样。当他们使用Sun编写OpenStep规范时(不要与OpenStep操作系统混淆),他们使用了ns前缀,就像在nsObject中那样。


基本上,ns源于nextstep,这是苹果收购next后成为Mac OS X的原始操作系统。

我想解释一些其他的事情,这就是为什么需要它。

在C++中有名字空间,几乎任何东西都在性病中。

这就是为什么您有std::string。

使用了名称空间,因此很难出错,您可以编写自己的类字符串,而不会与系统字符串冲突。

objective-c是c的超集,但它不包含名称空间,而且出于同样的原因,首先所有系统类都用ns或其他奇怪的prefix进行了prefix。

这与所有directx类如何用d3d预先固定以及所有opengl类如何用gl预先固定是相同的。

这意味着您不应该使用ns来命名自己的类,当您在核心动画中看到ns、ca或核心图形中看到cg时,您会理解这是对系统框架的调用。

Swift改变了这一惯例,因为Swift支持名称间距,并将其核心类型(如字符串)映射到ns等价物。


Bill Bumgarner aka@bbum,应该知道,2005年发布在CocoAbuilder邮件列表上:

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...

似乎每个人都同意前缀nx(next)直到1993/1994年才被使用,苹果的文档说:

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.


NextStep. It's a holdover from when objective-c was created by NeXT to
write nextstep (later os x) and all associated apps, etc. They
represent elements that aren't part of the original C underpinning,
for example, there is no 'string' element in C, so in objective-c,
it's an additional foundation element 'NSString'.