清理与结构的绑定

发布于 2024-08-30 22:04:53 字数 199 浏览 2 评论 0原文

我有一个模型类,对于它来说,拥有 NSSize 和 NSPoint 实例变量非常有意义。这很可爱。

我正在尝试为此对象创建一个编辑界面。我想绑定到 size.width 等等。这当然是行不通的。

解决这个问题最干净、最可可的解决方案是什么?当然,我可以为我使用的每个结构的各个成员编写单独的访问器,但似乎应该有更好的解决方案。

I have a model class for which it makes quite a lot of sense to have NSSize and NSPoint instance variables. This is lovely.

I'm trying to create an editing interface for this object. I'd like to bind to size.width and whatnot. This, of course, doesn't work.

What's the cleanest, most Cocoa-y solution to this problem? Of course I could write separate accessors for the individual members of every struct I use, but it seems like there should be a better solution.

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

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

发布评论

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

评论(1

撩起发的微风 2024-09-06 22:04:53

您不必为所有成员创建单独的访问器,您只需为您关心的类型创建包装器,例如:

@interface SizeWrapper : NSObject {
    CGFloat width, height;
}    
@property (readwrite) CGFloat width, height;    
- (id)initWithSize:(NSSize)sz;    
- (NSSize)sizeValue;
@end

@implementation SizeWrapper
@synthesize width, height;

- (id)initWithSize:(NSSize)sz {
    if (self = [super init]) {
        width  = sz.width;
        height = sz.height;
    }
    return self;
}

- (NSSize)sizeValue {
    return NSMakeSize(width, height);
}
@end

You don't have to create seperate accessors for all the members, you could just create wrappers for the types you care about, e.g.:

@interface SizeWrapper : NSObject {
    CGFloat width, height;
}    
@property (readwrite) CGFloat width, height;    
- (id)initWithSize:(NSSize)sz;    
- (NSSize)sizeValue;
@end

@implementation SizeWrapper
@synthesize width, height;

- (id)initWithSize:(NSSize)sz {
    if (self = [super init]) {
        width  = sz.width;
        height = sz.height;
    }
    return self;
}

- (NSSize)sizeValue {
    return NSMakeSize(width, height);
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文