这是我的情况:
我有一个带有类方法的协议,它解析一个 NSDictionary 并返回一个执行我的协议的对象:
@protocol RCParsableObject <NSObject>
/**
Parses an object from an NSDictionary representation
Return: A object created from the NSDictionary representation
*/
+(id<RCParsableObject>)parseObjectFromDictonary:(NSDictionary*)object;
@end
在我的代码的一部分,我有一个在 NSString 中执行该协议的类的名称:
//dict is the dictionary I want to parse
Class class= NSClassFromString(type);
if ([class conformsToProtocol:@protocol(RCParsableObject)]) {
//NSObject* object = [class parseObjectFromDictionary:dict];//Obviously that don't works but shows what I want
}
如果我想执行协议的 Class 方法,我知道它可以执行,因为 if。我怎么能这样做?它相当于:
NSObject * object = [class performSelector:_selector];
对于一个类对象?