绑定到 Cocoa 中的类方法?

发布于 2024-11-02 05:42:23 字数 517 浏览 5 评论 0原文

如果我有这样的方法:

@interface CharacterSet
    + (NSArray *)allCharacterSets;
@end

我可以使用 Cocoa 绑定来绑定它吗?

我正在尝试将 NSComboBox 的内容值连接到它。当我在 IB 的“模型密钥路径”字段中输入 CharacterSet.allCharacterSets 时,它不起作用,并显示:

[ 添加观察者: forKeyPath:@"CharacterSet.allCharacterSets" 选项:0x0 上下文:0x200275b80] 是 发送到一个不是的对象 “CharacterSet”符合 KVC 标准 财产。

我不知道还能尝试什么。目前,我必须将 allCharacterSets 的返回值存储到自定义窗口控制器(或自定义窗口)中的 ivar 中才能使其正常工作,这似乎是我不必采取的额外步骤。

If I have a method like:

@interface CharacterSet
    + (NSArray *)allCharacterSets;
@end

Can I bind to it using Cocoa bindings?

I'm trying to hook up an NSComboBox's content values to it. When I enter CharacterSet.allCharacterSets into the "Model Key Path" field in IB, it doesn't work, saying:

[
addObserver:
forKeyPath:@"CharacterSet.allCharacterSets"
options:0x0 context:0x200275b80] was
sent to an object that is not
KVC-compliant for the "CharacterSet"
property.

I'm not sure what else to try. Currently I have to store the return value of allCharacterSets into an ivar in my custom window controller (or custom window) to make it work, which seems like an extra step I shouldn't have to take.

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

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

发布评论

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

评论(2

旧竹 2024-11-09 05:42:23

我正在开发一个巨大的应用程序,你提到的额外步骤对我来说真的很痛苦。所以我开发了一个小的代理类来处理类方法绑定。

首先,我添加了一个小型代理类

@implementation INClassProxy

- (id)valueForUndefinedKey:(NSString *)key {
    return NSClassFromString(key);
}

@end

,然后向 NSApplication 添加了一个类别访问器

- (id)classProxy {
    static INClassProxy *proxy = nil;
    if (proxy == nil)
        proxy = [[INClassProxy alloc] init];
    return proxy;
}

(我实际上将其添加到我的应用程序委托中并实现了 application:delegateHandlesKey:

现在您已准备好即使在界面构建器中,也可以使用 keyPath @"classProxy.CharacterSet.allCharacterSets" 将类方法绑定到 Application 对象。

I'm working on a huge application and the extra steps you mentioned were really pain in the ass for me. So I developed a small proxy class that handles class method bindings.

First I've added a small proxy class

@implementation INClassProxy

- (id)valueForUndefinedKey:(NSString *)key {
    return NSClassFromString(key);
}

@end

Then added a category accessor to NSApplication

- (id)classProxy {
    static INClassProxy *proxy = nil;
    if (proxy == nil)
        proxy = [[INClassProxy alloc] init];
    return proxy;
}

(I actually added this to my application delegate and implemented application:delegateHandlesKey:)

Now you are ready to bind class methods to the Application object, even in the interface builder, with the keyPath @"classProxy.CharacterSet.allCharacterSets".

卸妝后依然美 2024-11-09 05:42:23

@cocoafan ..我在我热情洋溢的评论中意识到..我所说的例子可能行不通,除非你像我选择的那样实际实现你的天才答案......为了使这个令人头脑麻木的辉煌(以及简单)魔法更加通用和有用......我刚刚创建了一个基本类,正如你所描述的,但不是分类(这就是它的名字吗?)NSApplication,我是在 NSObject 上完成的>。下面是完整的例子…

@interface AZClassProxy : NSObject
@end
@interface NSObject (AZClassProxy)
- (id)classProxy;
@end

@implementation AZClassProxy
- (id) valueForUndefinedKey:(NSString*)k { return NSClassFromString(k); }
@end
@implementation NSObject (AZClassProxy)
- (id) classProxy { static AZClassProxy *prx = nil; return prx = prx ?: AZClassProxy.new; }
@end

bound by class

编辑…  大约 2 天后,我简单的思维折叠起来,试图记住这是谁的作品,以及为什么它如此精彩。  我会继续在这里发帖,因为我必须做一些事情来提醒自己这是什么,以及为什么我应该关心..  这是一个基本的例子..

NSObject *WHATEVS = NSObject.new;
NSLog(@"%@", [[WHATEVS.classProxy valueForKey:@"NSColor"] redColor]);

LOGNSCaliberatedRGBColorSpace 1 0 0 1

@cocoafan.. I realized in my effusive comment.. that the example I stated may not work unless you were to actually implement your GENIUS answer as I had chosen to… In order to make this mind-numbingly brilliant (as well as simple) magic even more generic and useful… I just created a basic class as you had described, but instead of categorizing (is that what it's called?) NSApplication, i did it on NSObject. Full example below…

@interface AZClassProxy : NSObject
@end
@interface NSObject (AZClassProxy)
- (id)classProxy;
@end

@implementation AZClassProxy
- (id) valueForUndefinedKey:(NSString*)k { return NSClassFromString(k); }
@end
@implementation NSObject (AZClassProxy)
- (id) classProxy { static AZClassProxy *prx = nil; return prx = prx ?: AZClassProxy.new; }
@end

bound by class

Edit…  After like 2 days my simple mind folded trying to remember who this works, and why it's so wonderful.  I will keep posting here as I have to do things to remind myself of what this is, and why I should care..  So a basic example..

NSObject *WHATEVS = NSObject.new;
NSLog(@"%@", [[WHATEVS.classProxy valueForKey:@"NSColor"] redColor]);

LOGNSCalibratedRGBColorSpace 1 0 0 1

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