ios NS前缀是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/473758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:34:38  来源:igfitidea点击:

What does the NS prefix mean?

iosobjective-ccocoacocoa-touch

提问by Martin08

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

Cocoa/Cocoa Touch 中的许多类都有 NS 前缀。这是什么意思?

采纳答案by Olaf Kock

It's from the NeXTSTEPheritage.

它来自NeXTSTEP遗产。

回答by Barry Wark

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

Cocoa 框架的原始代码来自NeXTSTEP库 Foundation 和 AppKit(Apple 的 Cocoa 框架仍在使用这些名称),NextStep 工程师选择在符号前加上 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.

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

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 pagewhere many developers in the Cocoa community have listed their "chosen" prefixes. You may also find this SO discussionhelpful.

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

回答by David Holm

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

ñEXT小号TEP或ñeXTSTEP /小号联合国取决于你问起是谁。

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

Sun 有一段时间对 OpenStep 进行了相当大的投资。在 Sun 进入图片之前,Foundation 中的大部分内容,尽管当时还没有被称为 Foundation,但前缀为NX,表示Ne XT,并且在 Sun 进入图片之前的某个时候,所有内容都被重命名为NS。在小号很可能没有代表太阳,然后,但太阳在普遍共识加强后,它主张太阳履行它们的参与。

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.

我实际上有一个参考,但我现在找不到它。如果/当我再次找到它时,我会更新帖子。

回答by Gregor

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.

它是 NextStep (= NS) 的遗产。NeXT 是史蒂夫乔布斯在 1985 年离开苹果公司后成立的计算机公司,NextStep 是它的操作系统(基于 UNIX)以及 Obj-C 语言和运行时。连同它的库和工具,NextStep 后来更名为 OpenStep(这也是 NeXT 与 Sun 共同开发的 API 上的名称),后来又变成了 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

合并开放步骤混乱

回答by Jonathan Lin

From Apple's developer docs:

来自 Apple 的开发人员文档:

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.

历史注释:如果您想知道为什么您遇到的这么多类都有 NS 前缀,那是因为 Cocoa 和 Cocoa Touch 的过去历史。Cocoa 最初是用于为 NeXTStep 操作系统构建应用程序的收集框架。当 Apple 于 1996 年购买 NeXT 时,NeXTStep 的大部分内容都被并入了 OS X,包括现有的类名。Cocoa Touch 是作为 Cocoa 的 iOS 版本推出的;Cocoa 和 Cocoa Touch 中都有一些类,尽管每个平台也有大量独特的类。两个字母的前缀,如 NS 和 UI(用于 iOS 上的用户界面元素),保留供 Apple 使用。

Source: Programming with Objective-C

资料来源:Objective-C 编程

回答by chown

From Cocoa_(API) Wikipedia:

来自Cocoa_(API) 维基百科

(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 NeXTand Sun.

Cocoa 类以首字母缩略词“NS”开头(代表OpenStepNeXT-Sun 创建,或代表 OpenStep 框架的原始专有术语NeXTSTEP):NSString、NSArray 等。

Foundation Kit,或者更常见的简称Foundation,首先出现在 OpenStep 中。在 Mac OS X 上,它基于 Core Foundation。Foundation 是一个通用的面向对象库,提供字符串和值操作、容器和迭代、分布式计算、运行循环和其他不直接绑定到图形用户界面的功能。“NS”前缀,用于框架中的所有类和常量,来自Cocoa 的 OPENSTEP 遗产,由NeXTSun共同开发

回答by chown

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.

当 NeXT 定义 NextStep API(与 NEXTSTEP 操作系统相反)时,他们使用前缀 NX,如 NXConstantString。当他们使用 Sun 编写 OpenStep 规范时(不要与 OPENSTEP 操作系统混淆),他们使用 NS 前缀,就像在 NSObject 中一样。

回答by Daniel Georgiev

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

基本上NS来自ñ分机小号TEP,原来的操作系统,成为Mac OS X中,当苹果收购下一步。

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

在 C++ 中有命名空间,几乎所有东西都在std 中

This is why you have std::string.

这就是为什么你有 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.

Objective-C 是 C 的超集,但它不包括命名空间,出于同样的原因,所有系统类都以 NS 或其他一些奇怪的前缀为前缀。

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

这与所有 DirectX 类如何以D3D为前缀以及所有 OpenGL 类如何以gl为前缀是相同的。

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

这意味着您不应该使用NS来命名您自己的类,当您在 Core Animation 中看到NSCA或在 Core Graphics 中看到CG 时,您就会明白这是对系统框架的调用。

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

Swift 改变了这个约定,因为 Swift 支持命名空间并且它将其核心类型(如 String)映射到 NS 等价物。

回答by Simon Woodside

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

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

在 NS 前缀开始发挥作用后,Sun 进入了画面。NS 前缀出现在从 NeXTSTEP 3.0 到 NeXTSTEP 4.0(也称为 OpenStep)的迁移过程中的公共 API 中。在 4.0 之前,少数符号使用 NX 前缀,但系统库提供的大多数类根本没有前缀——列表、哈希表、视图等......

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

似乎每个人都同意前缀 NX(用于 NeXT)一直使用到 1993/1994,Apple 的文档说

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.

1994 年 9 月发布的官方 OpenStep API 是第一个在 Foundation 和 Application Kit 之间拆分 API 的人,也是第一个使用“NS”前缀的人。