user635064 user635064 - 4 months ago 18
Objective-C Question

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"]
#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!

Answer

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
Comments