简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM