哪里是在iOS应用程序中存储常量的最佳位置? [英] Where's the best place to store constants in an iOS app?

查看:118
本文介绍了哪里是在iOS应用程序中存储常量的最佳位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

all of the resources have the same base URL:

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

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

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.

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

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

任何建议赞赏

推荐答案

我同意Alex Coplan的回答一个重要的补充。

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

将所有常量放在名为Constants.h的文件中(或者w /你想要的)

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


  • 当我回答这个问题三年时以前,我在 #define 上,请查看下面的修订版。

  • When I answered this question three years ago, I was on the #define bandwagon, check below for a revision.

Constants.h

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



尽管以前的所有信息仍然有用,但我们可以采取一些措施来保证我们的常数更加安全。

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.

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

Create your constants in your Constants.h file using const variables

//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;

并且在 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_EXPORT vs extern 。第一个为C和C ++编译不同。它基本上意味着 extern ,但在C ++中会添加C标志。

  • consts vs 定义 consts 是类型安全的并且尊重范围。 定义正好相反。

  • FOUNDATION_EXPORT vs extern. The first one compiles different for C and C++. It basically means extern, but in C++ will add the "C" flag.
  • consts vs defines. consts are type safe and respect scope. defines are the exact opposite.

这篇关于哪里是在iOS应用程序中存储常量的最佳位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆