在 iOS 应用程序中存储常量的最佳位置在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9250805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:47:14  来源:igfitidea点击:

Where's the best place to store constants in an iOS app?

objective-cios

提问by bodacious

I'm developing an app just now that fetches resources from a JSON API.

我刚刚正在开发一个从 JSON API 获取资源的应用程序。

all of the resources have the same base URL:

所有资源都具有相同的基本 URL:

http://api.mysite.com/resources.json
http://api.mysite.com/other_resources.json

I want to store the http://api.mysite.com/string so it's available to all of my Controllers and Models, removing some duplication when writing the resource URLs.

我想存储http://api.mysite.com/字符串,以便它可用于我的所有控制器和模型,在编写资源 URL 时删除一些重复项。

Where's the best place to do this? The -prefix.pchfile?

最好的地方在哪里?该-prefix.pch文件?

Any advice appreciated

任何建议表示赞赏

回答by ColdLogic

I agree with Alex Coplan's answer with an important addition.

我同意 Alex Coplan 的回答,并补充了一个重要信息。

Put all your constants in a file named "Constants.h" (or w/e you want)

将所有常量放在一个名为“Constants.h”的文件中(或者你想要的)

EDIT:

编辑:

  • When I answered this question three years ago, I was on the #definebandwagon, check below for a revision.
  • 三年前,当我回答这个问题时,我正#define赶上潮流,请查看下面的修订版。

Constants.h

常量.h

#define kFilterDate @"date"
#define kFilterRadius @"radius"
#define kFilterSort @"sort"

//Global Strings
#define kDividingString @" / "

//Strings
#define kTour @"Tour"
#define kToursKey @"tours"

But instead of importing it in any file you need it, import it in your prefix file so that all of your headers import it automatically throughout your project.

但是不要将它导入到您需要的任何文件中,而是将它导入到您的前缀文件中,以便您的所有标头在整个项目中自动导入它。

Project_Prefix.pch

Project_Prefix.pch

//
// Prefix header for all source files of the project
//

#ifdef __OBJC__
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    #import "Constants.h"
#endif

REVISION

修订

All though all the previous information will still work, there are some things we can do to be a little bit more safe about our constants.

尽管所有以前的信息仍然有效,但我们可以做一些事情来使我们的常量更加安全。

Create your constants in your Constants.hfile using const variables

Constants.h使用常量变量在文件中创建常量

//Filters
FOUNDATION_EXPORT NSString *const kFilterDate;
FOUNDATION_EXPORT NSString *const kFilterRadius;
FOUNDATION_EXPORT NSString *const kFilterSort;

//Global Strings
FOUNDATION_EXPORT NSString *const kDividingString;

//Strings
FOUNDATION_EXPORT NSString *const kTour;
FOUNDATION_EXPORT NSString *const kToursKey;

And in Constants.m

而在 Constants.m

//Filters
NSString *const kFilterDate = @"date";
NSString *const kFilterRadius = @"radius";
NSString *const kFilterSort = @"sort";

//Global Strings
NSString *const kDividingString = @" / ";

//Strings
NSString *const kTour = @"Tour";
NSString *const kToursKey = @"tours";

This can still be imported into your prefix file like above, but only use constants that are truly global in the file to do that. Ones that are used frequently in many places. Dumping all your constants into this file will cause your code that uses any constant to be coupled to the constants file. Thus, if you try to reuse the code, the constants file has to come with it. This isn't always necessarily bad, and many times is intended (which is fine), but limiting dependencies is always a good idea.

这仍然可以像上面一样导入到您的前缀文件中,但只能使用文件中真正全局的常量来执行此操作。在很多地方经常使用的。将所有常量转储到此文件中将导致使用任何常量的代码耦合到常量文件。因此,如果您尝试重用代码,则必须附带常量文件。这并不总是很糟糕,而且很多时候是有意的(这很好),但限制依赖总是一个好主意。

A few things about the revision:

关于修订的一些事情:

  • FOUNDATION_EXPORTvs extern. The first one compiles different for C and C++. It basically means extern, but in C++ will add the "C" flag.
  • constsvs defines. constsare type safe and respect scope. definesare the exact opposite.
  • FOUNDATION_EXPORTextern. 第一个对 C 和 C++ 编译不同。它基本上意味着extern,但在 C++ 中会添加“C”标志。
  • constsdefines. consts是类型安全和尊重范围。defines恰恰相反。

回答by UIAdam

Personally I prefer using actual const variables rather than defines.

我个人更喜欢使用实际的 const 变量而不是定义。

In a MyConstants.m file I have:

在 MyConstants.m 文件中,我有:

NSString *const kXYMySiteBaseURL = @"http://api.mysite.com/";
NSString *const kXYSomeOtherURL = @"http://www.google.com/";

where XY is my initials or some other "unique" prefix to avoid collisions with other constants.

其中 XY 是我的首字母或其他一些“唯一”前缀,以避免与其他常量发生冲突。

Then I have a MyConstants.h file like this:

然后我有一个像这样的 MyConstants.h 文件:

extern NSString *const kXYMySitBaseURL;
extern NSString *const kXYSomeOtherURL;

Depending on how many files need to access these constants, I might include it in the precompiled header like ColdFusion suggests in his answer.

根据需要访问这些常量的文件数量,我可能会将它包含在预编译头文件中,就像 ColdFusion 在他的回答中建议的那样。

This is how Apple defines their constants in most of the Core frameworks.

这就是 Apple 在大多数 Core 框架中定义其常量的方式。

回答by Alex Coplan

I just create a file called Globals.hwith something like the following:

我只是创建了一个名为的文件Globals.h,如下所示:

#define kBaseURL @"http://api.mysite.com/"

Then to use:

然后使用:

#import "Globals.h" // at the top

NSString *url = [NSString stringWithFormat:@"%@resources.json",kBaseURL];

回答by Oscar Gomez

I would create a singleton or use the AppDelegate and put the constants there.

我会创建一个单例或使用 AppDelegate 并将常量放在那里。

回答by Half_Duplex

Yes, a global header would be an ideal solution. I wouldn't go as far as a singleton pattern unless plan on using it for other things like managing your data store. A singleton for globals is somewhat overkill.

是的,全局标题将是一个理想的解决方案。除非计划将其用于管理数据存储等其他事情,否则我不会采用单例模式。全局变量的单例有点矫枉过正。