Cocoa/Cocoa Touch中的许多类都有NS前缀。这是什么意思?
当前回答
来自Cocoa_(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.
其他回答
来自Cocoa_(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.
Cocoa框架的原始代码来自NeXTSTEP库基金会和AppKit(这些名称仍然被苹果的Cocoa框架使用),NeXTSTEP的工程师选择用NS作为他们的符号前缀。
因为Objective-C是C的扩展,因此不像c++那样有名称空间,符号必须有一个唯一的前缀,这样它们就不会发生冲突。这对于在框架中定义的符号尤为重要。
如果您正在编写一个应用程序,以便您的代码可能只使用您的符号,那么您不必担心这个问题。但是如果您正在编写供他人使用的框架或库,您还应该为您的符号添加一个唯一的前缀。CocoaDev有一个页面,许多Cocoa社区的开发人员列出了他们“选择”的前缀。您可能也会发现这个SO讨论很有帮助。
它来自NeXTSTEP传统。
当NeXT定义NextStep API(与NextStep操作系统相反)时,他们使用了前缀NX,如NXConstantString。当他们使用Sun(不要与OpenStep操作系统混淆)编写OpenStep规范时,他们使用了NS前缀,就像在NSObject中一样。
苹果的开发者文档如下:
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编程
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- 数据挖掘中分类和聚类的区别?
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- 为什么ARC仍然需要@autoreleasepool ?
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 如何从UIImage (Cocoa Touch)或CGImage (Core Graphics)获取像素数据?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- Xcode 4挂在“附加到(应用程序名称)”
- 主体、使用者和主体之间的意义和区别是什么?