Cocoa 绑定和宏

发布于 2024-11-02 02:34:20 字数 952 浏览 6 评论 0原文

我在 Interface Builder 中有一堆绑定。当我想访问它时,我必须这样做,例如:

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

并且我必须为其他绑定执行至少 20 次...那么,如果我使用 #define 来正常吗?这些绑定是这样的:

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

它工作得很好,但我应该使用这种方式吗?我还有更好的办法吗? 谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2024-11-09 02:34:20

我从来没有这样做过,但我认为你的方法没有理由行不通。但是,我认为创建静态实用程序类可能更灵活且更易于维护,例如:

@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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文