开发者

Cocoa bindings and macros

I have a bunch of bindings in Interface Builder. When I want to access it, I have to do this, for example:

[[NSUserDefaults standardUserDefaults] integerForKey:@"bINDDPagesTag"]

and I have to do it for other bindings at least 20 times...so, is it normal if I using #define for these bindings like this:

#define kINDDPagesTag [[NSUserDefaults standardUserDefaults] integerForKey:@"bINDDPagesTag"]
#define kINDDRange [[NSUserDefaults standardUserDefaults] objectForKey:@"bINDDRange"]
#define kINDDBleedTop [[NSUserDefaults standardUserDefaults] objectForKey:@"bINDDBleedTop"]
#define kINDDBleedBottom [[NSUserDefaults standardUserDefaults] objectForKey:@"bINDDBleedBottom"]
#define kINDDBleedInside [[NSUserDefaults standardUserDefaults] objectForKey:@"bINDDBleedInside"]开发者_如何学C
#define kINDDBleedOutside [[NSUserDefaults standardUserDefaults] objectForKey:@"bINDDBleedOutside"]
#define kINDDSendData [[NSUserDefaults standardUserDefaults] objectForKey:@"bINDDSendData"]

It works perfectly, but should I use this way? I there a better way? Thanks!


I've never done it that way, but I see no reason why your approach wouldn't work. However, I think creating a static utility class may be a bit more flexible and more maintainable, like:

@interface InterfaceConstants : NSObject {
}
+ (int)pagesTag;
+ (id)range;
//etc.
@end

@implementation InterfaceConstants
+ (int)pagesTag {
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"bINDDPagesTag"];
}
+ (id)range {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"bINDDRange"];
}
//etc.
@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜