Cocoa/Cocoa Touch中的许多类都有NS前缀。这是什么意思?
当前回答
NS基本上来自NextSTEP,这是苹果收购Next时最初的操作系统,后来成为Mac OS X。
我想解释一些其他的东西,这就是为什么需要它。
在c++中有命名空间,几乎所有的东西都放在std中
这就是为什么要使用std::string。
使用名称空间,这样就不会出错,而且可以编写自己的类字符串,而不会与系统字符串冲突。
Objective-C是C的超集,但它不包括命名空间,出于同样的原因,所有系统类都以NS或其他奇怪的前缀作为前缀。
这与所有DirectX类都以D3D为前缀,以及所有OpenGL类都以gl为前缀是一样的。
这意味着你不应该使用NS来命名你自己的类,当你在Core Animation中看到NS, CA或在Core Graphics中看到CG时,你就知道这是对系统框架的调用。
Swift改变了这一惯例,因为Swift支持命名空间,并将其核心类型(如String)映射到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.
苹果的开发者文档如下:
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编程
NS基本上来自NextSTEP,这是苹果收购Next时最初的操作系统,后来成为Mac OS X。
我想解释一些其他的东西,这就是为什么需要它。
在c++中有命名空间,几乎所有的东西都放在std中
这就是为什么要使用std::string。
使用名称空间,这样就不会出错,而且可以编写自己的类字符串,而不会与系统字符串冲突。
Objective-C是C的超集,但它不包括命名空间,出于同样的原因,所有系统类都以NS或其他奇怪的前缀作为前缀。
这与所有DirectX类都以D3D为前缀,以及所有OpenGL类都以gl为前缀是一样的。
这意味着你不应该使用NS来命名你自己的类,当你在Core Animation中看到NS, CA或在Core Graphics中看到CG时,你就知道这是对系统框架的调用。
Swift改变了这一惯例,因为Swift支持命名空间,并将其核心类型(如String)映射到NS等等物。
它来自NeXTSTEP传统。
当NeXT定义NextStep API(与NextStep操作系统相反)时,他们使用了前缀NX,如NXConstantString。当他们使用Sun(不要与OpenStep操作系统混淆)编写OpenStep规范时,他们使用了NS前缀,就像在NSObject中一样。
推荐文章
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue
- 编程中的术语“上下文”?
- 在Objective-C中@synchronized如何锁定/解锁?
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。