What does it mean in Objective-C if you try to mix Class and Protocol? I'm trying to get the complier to warn me at compiler time if a class doesn't conform to a protocol. For example:
如果你试图混合类和协议,那么Objective-C是什么意思呢?我试图让编译器在编译时警告我,如果一个类不符合协议。例如:
@protocol FLLiveDataProtocol <NSObject>
...
@end
- (id)initWithDataPath:(NSString *)path usingDataClassFactory:(Class<FLLiveDataProtocol>)dataFactoryClass;
However, I get all kind of interesting issues when I try this. First, it doesn't trigger a compiler error if you pass in a class that doesn't conform to the protocol, second you will get errors such as "No know class method for selector alloc" if you try to do [[dataFactoryClass] alloc] init] even through the protocol conforms to NSObject.
然而,当我尝试这个的时候,我遇到了各种有趣的问题。首先,如果您在一个不符合协议的类中传递,它不会触发编译器错误,第二,如果您尝试执行[[dataFactoryClass] alloc] init],即使通过协议也符合NSObject,您也会得到错误,例如“不知道选择器alloc的类方法”。
I haven't seen many examples of people trying to mix protocol's and the Class keyword (I should say Typedef -- thanks Nikolai).
我没有见过很多人试图混合协议和Class关键字(我应该说Typedef——谢谢Nikolai)的例子。
I know I can use conformsToProtocol to check at runtime, but it would be nice if I could get this to check at compile time.
我知道我可以使用conformsToProtocol在运行时进行检查,但是如果我能在编译时检查一下就更好了。
2 个解决方案
#1
7
There's no static type information for class objects in Objective-C (see this question), so there's no way to express the need for a class to conform to a protocol.
在Objective-C中没有类对象的静态类型信息(请参见这个问题),因此没有办法表示需要一个类符合协议。
Side note: Class
is not a keyword but a typedef in objc.h:
附注:类不是关键字,而是objc中的typedef。
typedef struct objc_class *Class;
#2
1
second you will get errors such as "No know class method for selector alloc" if you try to do [[dataFactoryClass] alloc] init] even through the protocol conforms to NSObject.
其次,如果您尝试执行[[dataFactoryClass] alloc] init],即使通过协议遵守NSObject,也会出现诸如“选择器alloc的不知道类方法”之类的错误。
The reason for this is that neither your protocol or the NSObject protocol declare +alloc
. You could fix this by adding an +alloc
method (or some other class method that returns a new instance) to your protocol declaration.
原因是您的协议或NSObject协议都没有声明+alloc。可以通过向协议声明中添加+alloc方法(或返回新实例的其他类方法)来解决这个问题。
#1
7
There's no static type information for class objects in Objective-C (see this question), so there's no way to express the need for a class to conform to a protocol.
在Objective-C中没有类对象的静态类型信息(请参见这个问题),因此没有办法表示需要一个类符合协议。
Side note: Class
is not a keyword but a typedef in objc.h:
附注:类不是关键字,而是objc中的typedef。
typedef struct objc_class *Class;
#2
1
second you will get errors such as "No know class method for selector alloc" if you try to do [[dataFactoryClass] alloc] init] even through the protocol conforms to NSObject.
其次,如果您尝试执行[[dataFactoryClass] alloc] init],即使通过协议遵守NSObject,也会出现诸如“选择器alloc的不知道类方法”之类的错误。
The reason for this is that neither your protocol or the NSObject protocol declare +alloc
. You could fix this by adding an +alloc
method (or some other class method that returns a new instance) to your protocol declaration.
原因是您的协议或NSObject协议都没有声明+alloc。可以通过向协议声明中添加+alloc方法(或返回新实例的其他类方法)来解决这个问题。