如何在iOS objective-c代码中使用名称空间?

时间:2022-09-07 09:38:42

I'm writing an iOS app, "Best Korea". My organization name is "Srsly.co". I'm going to write re-usable "News" libraries that I'll use across my apps.

我正在写一个iOS应用程序,“最好的韩国”。我的组织名称是“Srsly.co”。我将编写可重用的“新闻”库,我将在我的应用程序中使用这些库。

Each iOS app will have its own app-wide constants in a .h file, and the library code will have its constants as well in header files. I'll also have tests for each of these projects.

每个iOS应用程序在.h文件中都有自己的应用程序范围的常量,库代码也会在头文件中有其常量。我还将对每个项目进行测试。

Is this the standard way of doing things?

这是标准的做事方式吗?

In Ruby, Python, Java, etc., I'd set up namespaces along these lines:

在Ruby,Python,Java等中,我按照这些方式设置名称空间:

co.srsly.bestkorea
co.srsly.bestkorea.test
co.srsly.newslib
co.srsly.newslib.test

As far as I can see, the Objective-C pattern is for each developer to choose two or three upper-case letters and prefix every class name with them.

据我所知,Objective-C模式是为每个开发人员选择两个或三个大写字母并为每个类名添加前缀。

So in my case, I'm thinking I'd choose BK as the app's classname prefix and NL for the news lib code? Am I thinking about this the right way?

所以在我的情况下,我认为我会选择BK作为应用程序的类名前缀和新闻lib代码的NL?我是否正确地思考这个问题?

EDIT: I'm considering not using namespacing at all in my application code as discussed here.

编辑:我正在考虑不在我的应用程序代码中使用命名空间,如此处所述。

2 个解决方案

#1


33  

You're correct that Objective-C doesn't have built in support for namespaces, and the common solution is to use uppercase prefixes on each class. Note that Apple has stated that two letter prefixes are reserved for their use, so you should use three letter prefixes for your own classes. Otherwise, your suggested approach is the normal thing to do.

你是对的,Objective-C没有内置的名称空间支持,常见的解决方案是在每个类上使用大写前缀。请注意,Apple已声明保留两个字母前缀供其使用,因此您应为自己的类使用三个字母前缀。否则,您建议的方法是正常的做法。

#2


2  

There is no NameSpace in Objective-C as you are expecting in Java.

Objective-C中没有NameSpace,正如您在Java中所期望的那样。

Objective-C uses class Prefix like NS, UI, CG, CF etc to safely remove name space collision.

Objective-C使用NS,UI,CG,CF等类前缀来安全地删除名称空间冲突。

And it would be better to use 3 letter Prefix for your class.

最好为你的班级使用3个字母的前缀。

You should read this : What is the best way to solve an Objective-C namespace collision?

您应该阅读:解决Objective-C命名空间冲突的最佳方法是什么?

#1


33  

You're correct that Objective-C doesn't have built in support for namespaces, and the common solution is to use uppercase prefixes on each class. Note that Apple has stated that two letter prefixes are reserved for their use, so you should use three letter prefixes for your own classes. Otherwise, your suggested approach is the normal thing to do.

你是对的,Objective-C没有内置的名称空间支持,常见的解决方案是在每个类上使用大写前缀。请注意,Apple已声明保留两个字母前缀供其使用,因此您应为自己的类使用三个字母前缀。否则,您建议的方法是正常的做法。

#2


2  

There is no NameSpace in Objective-C as you are expecting in Java.

Objective-C中没有NameSpace,正如您在Java中所期望的那样。

Objective-C uses class Prefix like NS, UI, CG, CF etc to safely remove name space collision.

Objective-C使用NS,UI,CG,CF等类前缀来安全地删除名称空间冲突。

And it would be better to use 3 letter Prefix for your class.

最好为你的班级使用3个字母的前缀。

You should read this : What is the best way to solve an Objective-C namespace collision?

您应该阅读:解决Objective-C命名空间冲突的最佳方法是什么?