ObjC:有“类协议"这样的东西吗? [英] ObjC: is there such a thing as a "class protocol"?

查看:43
本文介绍了ObjC:有“类协议"这样的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于对象实例,我们可以让它们的类声明一些协议一致性,如下所示:

For object instances we can have their class declare some protocol conformance as in:

@protocol P <NSObject>
- (void) someMethod ;
@end

@interface C : NSObject <P>
@end

@implementation C
- (void) someMethod {

}
@end

但是课程呢?

我发现自己处于这种情况:

I find myself in this situation:

...
Class c = [self modelClass:kind] ;
if (c) {
    model = [c performSelector: @selector(decode:) 
                    withObject: [SExpIO read: [fm contentsAtPath:target]]] ;
}

并且我希望有一种方法可以让我声明存在类方法协议这样的东西.

and I wish there were a way for me to declare that there is such a thing as protocols for class methods.

在上面的例子中,c 可以是类实例(嗯??)的所有类,声明

In the above example, all classes that c can be a class-instance (Hmmm??) of, declare

+ (id) decode: (SExp *) root ;

有什么办法可以将上述内容转换为:

Is there a way that I could transform the above into:

if (c) {
    model = [c decode: [SExpIO read: [fm contentsAtPath:target]]]
}

通过使用合适的类协议"声明?

by using a suitable "class protocol" declaration?

推荐答案

有诸如类方法的协议之类的东西,它们被称为....协议.例如,看起来您想要一个如下所示的协议:

There are such things as Protocols for class methods, and they're called.... Protocols. For example, it looks like you want a protocol that looks like this:

@protocol MyDecoder
+ (id)decode:(SExp *)root;
@end

然后你可以像这样使用它:

You can then use it like this:

Class c = [self modelClass:kind];
if ([c conformsToProtocol:@protocol(MyDecoder)]) {
     model = [c decode: [SExpIO read: [fm contentsAtPath:target]]];
}

这篇关于ObjC:有“类协议"这样的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆