我正在开发一个Cocoa应用程序,我使用常量nsstring作为存储我的首选项的键名的方式。
我知道这是一个好主意,因为它允许在必要时轻松更改密钥。 此外,这是整个“将数据与逻辑分离”的概念。
不管怎样,有没有一种好方法让这些常量在整个应用程序中定义一次?
我相信有一种简单而聪明的方法,但现在我的类只是重新定义它们所使用的方法。
我正在开发一个Cocoa应用程序,我使用常量nsstring作为存储我的首选项的键名的方式。
我知道这是一个好主意,因为它允许在必要时轻松更改密钥。 此外,这是整个“将数据与逻辑分离”的概念。
不管怎样,有没有一种好方法让这些常量在整个应用程序中定义一次?
我相信有一种简单而聪明的方法,但现在我的类只是重新定义它们所使用的方法。
当前回答
我通常使用Barry Wark和Rahul Gupta发布的方法。
虽然,我不喜欢在.h和.m文件中重复相同的单词。 注意,在下面的例子中,这一行在两个文件中几乎是相同的:
// file.h
extern NSString* const MyConst;
//file.m
NSString* const MyConst = @"Lorem ipsum";
因此,我喜欢使用一些C预处理器机制。 让我通过例子来解释。
我有一个头文件,它定义宏STR_CONST(名称,值):
// StringConsts.h
#ifdef SYNTHESIZE_CONSTS
# define STR_CONST(name, value) NSString* const name = @ value
#else
# define STR_CONST(name, value) extern NSString* const name
#endif
h/里的。m对,我想定义常数,我这样做:
// myfile.h
#import <StringConsts.h>
STR_CONST(MyConst, "Lorem Ipsum");
STR_CONST(MyOtherConst, "Hello world");
// myfile.m
#define SYNTHESIZE_CONSTS
#import "myfile.h"
等,瞧,我有关于常数的所有信息在.h文件只有。
其他回答
如果你喜欢命名空间常量,你可以使用struct,周五问答2011-08-19:命名空间常量和函数
// in the header
extern const struct MANotifyingArrayNotificationsStruct
{
NSString *didAddObject;
NSString *didChangeObject;
NSString *didRemoveObject;
} MANotifyingArrayNotifications;
// in the implementation
const struct MANotifyingArrayNotificationsStruct MANotifyingArrayNotifications = {
.didAddObject = @"didAddObject",
.didChangeObject = @"didChangeObject",
.didRemoveObject = @"didRemoveObject"
};
我使用了一个单例类,这样我就可以模拟该类并在测试时更改常量。常量类是这样的:
#import <Foundation/Foundation.h>
@interface iCode_Framework : NSObject
@property (readonly, nonatomic) unsigned int iBufCapacity;
@property (readonly, nonatomic) unsigned int iPort;
@property (readonly, nonatomic) NSString * urlStr;
@end
#import "iCode_Framework.h"
static iCode_Framework * instance;
@implementation iCode_Framework
@dynamic iBufCapacity;
@dynamic iPort;
@dynamic urlStr;
- (unsigned int)iBufCapacity
{
return 1024u;
};
- (unsigned int)iPort
{
return 1978u;
};
- (NSString *)urlStr
{
return @"localhost";
};
+ (void)initialize
{
if (!instance) {
instance = [[super allocWithZone:NULL] init];
}
}
+ (id)allocWithZone:(NSZone * const)notUsed
{
return instance;
}
@end
它是这样使用的(注意常量c的简写使用-它节省了每次输入[[constants alloc] init]):
#import "iCode_FrameworkTests.h"
#import "iCode_Framework.h"
static iCode_Framework * c; // Shorthand
@implementation iCode_FrameworkTests
+ (void)initialize
{
c = [[iCode_Framework alloc] init]; // Used like normal class; easy to mock!
}
- (void)testSingleton
{
STAssertNotNil(c, nil);
STAssertEqualObjects(c, [iCode_Framework alloc], nil);
STAssertEquals(c.iBufCapacity, 1024u, nil);
}
@end
如果你想调用nsstring。newline;从objective c,你想要它是静态常量,你可以在swift中创建这样的东西:
public extension NSString {
@objc public static let newLine = "\n"
}
这样你就有了很好的可读的常量定义,并且可以从你选择的类型中获得,同时还可以绑定到类型的上下文。
// Prefs.h
extern NSString * const RAHUL;
// Prefs.m
NSString * const RAHUL = @"rahul";
对@Krizz的建议进行了轻微修改,以便在常量头文件被包含在PCH中时正常工作,这是相当正常的。由于原始文件被导入到PCH中,它不会重新加载到.m文件中,因此您没有得到任何符号,链接器也不高兴。
但是,下面的修改允许它工作。这有点复杂,但很有效。
你需要3个文件,。h文件有常量定义,.h文件和。m文件,我将使用ConstantList.h, Constants.h和Constants。m,分别。Constants.h的内容很简单:
// Constants.h
#define STR_CONST(name, value) extern NSString* const name
#include "ConstantList.h"
和常数。M文件如下所示:
// Constants.m
#ifdef STR_CONST
#undef STR_CONST
#endif
#define STR_CONST(name, value) NSString* const name = @ value
#include "ConstantList.h"
最后,ConstantList.h文件中有实际的声明,这就是全部:
// ConstantList.h
STR_CONST(kMyConstant, "Value");
…
有几点需要注意:
我必须在.m文件中#undefing后重新定义宏。 我还必须使用#include而不是#import来正常工作,并避免编译器看到之前预编译的值。 这将需要在任何值发生更改时重新编译PCH(可能还包括整个项目),如果正常情况下将它们分开(并复制),则不会出现这种情况。
希望这对大家有所帮助。