在Cocoa或者Cocoa Touch中很多类都是以NS为前缀,为什么?有什么含义?
它是继承自 NeXTSTEP ,Cocoa框架的源代码很多来自NexSTEP库的Foundation和AppKit中(那些名字仍在Apple的Cocoa框架中使用),NextStep公司的工程师选择的NS作为他们的类前缀,因此使用到了现在。
因为Objective-C是C语言的扩展,它不想C++那样有命名空间,为了避免冲突,符号必须使用一个唯一的前缀。尤其在框架中定义一个符号时更应该这样。
如果你正在编写一个应用程序,这样你的代码只可能使用你的符号,你不必担心这个。但是,如果您正在编写供他人使用的框架或库,您还应该在符号前面加上惟一的前缀。 CocoaDev has a page社区中的许多开发人员都列出了他们“选择”的前缀。你也会发现this SO discussion的讨论很有帮助。
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.
也可以参考Cocoa_(API) Wikipedia:
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.