Objectiev-C 按Class类型调用函数(已知类型)

+ (GPBMessage*)disassembleBodyWithData:(Class)dataClass data:(NSData*)data {
    GPBMessage *model = nil;
    if (dataClass && [dataClass isSubclassOfClass:[GPBMessage class]]) {
        Method method = class_getClassMethod(dataClass, @selector(parseFromData:error:));
        IMP imp = method_getImplementation(method);
        if (imp) {
            SEL oriSEL = method_getName(method);
            if (oriSEL) {
                NSError *err = nil;
                GPBMessage*(*parseFunction)(__strong id, SEL, NSData*, NSError** ) = (GPBMessage* (*)(__strong id, SEL,  NSData*, NSError**))imp;//函数类型
                model = parseFunction(dataClass, oriSEL,data, &err);//函数指针
                if (err) {
                     NSLog(@"error: %@", err);
                }
            }
        }
    }
    return model;
}

你可能感兴趣的:(Objectiev-C 按Class类型调用函数(已知类型))