NS前缀表达的意思

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

参考资料

What does the NS prefix mean?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jarlen John

谢谢你给我一杯咖啡的温暖

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值