ios 如何声明和使用 NSString 全局常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11932969/
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
How to declare and use NSString global constants
提问by Anand
Possible Duplicate:
Constants in Objective C
可能的重复:
Objective C 中的常量
I store some app settings in NSUserDefaults. NSStrings are used as keys. The problem is I need to access these settings throughout the app using those NSString keys. There is a chance that I mistype such string key when accessing in some part of the app.
我在 NSUserDefaults 中存储了一些应用程序设置。NSStrings 用作键。问题是我需要使用这些 NSString 键在整个应用程序中访问这些设置。在应用程序的某些部分访问时,我可能会错误输入此类字符串键。
Throughout the app, I have such statements
在整个应用程序中,我有这样的陈述
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ReminderSwitch"];
BOOL shouldRemind = [[NSUserDefaults standardUserDefaults] boolForKey:@"ReminderSwitch"];
How and where can I declare a global NSString constant which I can access throughout the app. I will then be able to use that constant without worrying about mistyping those string keys.
我如何以及在哪里声明一个全局 NSString 常量,我可以在整个应用程序中访问它。然后我将能够使用该常量而不必担心输入错误的字符串键。
回答by justin
First, you should go for a real extern C symbol -- not a macro. this is done like so:
首先,您应该使用真正的 extern C 符号——而不是宏。这是这样做的:
SomeFile.h
一些文件
extern NSString *const MONConstantString;
SomeFile.m
一些文件
NSString *const MONConstantString = @"MONConstantString";
note that if you use a mix of ObjC and ObjC++, you will need to specify extern "C"
for C++ TUs -- that's why you will see a #define
d export which varies by language.
请注意,如果您混合使用 ObjC 和 ObjC++,则需要extern "C"
为 C++ TU指定——这就是为什么您会看到#define
因语言而异的d 导出。
Then, you will want to put the constant near the interfaces it relates to. Taking your example as a lead, you might want a set of interfaces or declarations for your app's preferences. In that case, you might add the declaration to MONAppsPreferences
header:
然后,您需要将常量放在与其相关的接口附近。以您的示例为例,您可能需要一组用于应用首选项的接口或声明。在这种情况下,您可以将声明添加到MONAppsPreferences
标题:
MONAppsPreferences.h
MONAppsPreferences.h
extern NSString *const MONApps_Pref_ReminderSwitch;
MONAppsPreferences.m
MONAppsPreferences.m
NSString *const MONApps_Pref_ReminderSwitch = @"MONApps_Pref_ReminderSwitch";
In use:
正在使用:
#import "MONAppsPreferences.h"
...
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:MONApps_Pref_ReminderSwitch];
回答by Feel Physics
Your thought is right I think. For example, I made Const.h/m file like below:
你的想法是对的,我认为。例如,我制作了如下所示的 Const.h/m 文件:
Const.h
常量
extern NSString *const UserIdPrefKey;
extern NSString *const PasswordPrefKey;
extern NSString *const HomepagePrefKey;
Const.m
常数
#import "AEConst.h"
NSString *const UserIdPrefKey = @"UserIdPrefKey";
NSString *const PasswordPrefKey = @"PasswordPrefKey";
NSString *const HomepagePrefKey = @"UrlHomepagePrefKey";
Only Const.h must be imported.
只有 Const.h 必须被导入。
When you write code, Xcode supports writing the key name so that you can avoid miss-typing.
当您编写代码时,Xcode 支持编写键名,这样您就可以避免输入错误。
回答by Erik S
What you seem to be looking for is just a way to define string constants in your app.
您似乎正在寻找的只是一种在您的应用程序中定义字符串常量的方法。
See this questionand this answerto it, which I've quoted below:
You should create a header file like
// Constants.h FOUNDATION_EXPORT NSString *const MyFirstConstant; FOUNDATION_EXPORT NSString *const MySecondConstant; //etc.
You can include this file in each file that uses the constants or in the pre-compiled header > for the project.
You define these constants in a .m file like
// Constants.m NSString *const MyFirstConstant = @"FirstConstant"; NSString *const MySecondConstant = @"SecondConstant";
Constants.m should be added to your application/framework's target so that it is linked in to the final product.
The advantage of using string constants instead of #define'd constants is that you can test for equality using pointer comparison
(stringInstance == MyFirstConstant)
which is much faster than string comparison([stringInstance isEqualToString:MyFirstConstant])
(and easier to read, IMO).
您应该创建一个头文件,如
// Constants.h FOUNDATION_EXPORT NSString *const MyFirstConstant; FOUNDATION_EXPORT NSString *const MySecondConstant; //etc.
您可以将此文件包含在每个使用常量的文件中,或者包含在项目的预编译头文件中。
您在 .m 文件中定义这些常量,例如
// Constants.m NSString *const MyFirstConstant = @"FirstConstant"; NSString *const MySecondConstant = @"SecondConstant";
Constants.m 应该添加到您的应用程序/框架的目标中,以便将其链接到最终产品。
使用字符串常量而不是 #define'd 常量的优点是您可以使用指针比较来测试相等性
(stringInstance == MyFirstConstant)
,这比字符串比较快得多([stringInstance isEqualToString:MyFirstConstant])
(并且更容易阅读,IMO)。
With thanks to Barry Wark :)
感谢巴里沃克 :)
回答by Artem Kalachev
The easiest way to do this is make simple .h file, like Utils.h and write there following code:
最简单的方法是制作简单的 .h 文件,如 Utils.h 并在那里编写以下代码:
#define kUserDefaults @"ReminderSwitch"
#define kUserDefaults @"ReminderSwitch"