FMWebViewJavascriptBridge icon indicating copy to clipboard operation
FMWebViewJavascriptBridge copied to clipboard

不能继承interface

Open TuWei1992 opened this issue 8 years ago • 3 comments

TuWei1992 avatar May 04 '17 01:05 TuWei1992

恩 目前是不支持,但是应该不会有那么深的接口层次吧

carlSQ avatar May 09 '17 07:05 carlSQ

@carlSQ 一般会做基础封装, 特别的业务需要定制, 在保证保持JS的命名空间一致的情况下, 就需要继承去扩展了. 这是我修改后的NSObject+FMAnnotation.h代码, 用递归实现了继承.

#import "NSObject+FMAnnotation.h"
#import <objc/runtime.h>

@implementation NSObject (FMAnnotation)
- (NSDictionary *)methodAnnotations {
    NSMutableDictionary *methodsMap = objc_getAssociatedObject(self, _cmd);
    if (methodsMap) {
        return methodsMap;
    }
    methodsMap = [self classExportedMethods:self.class];
    objc_setAssociatedObject(self, _cmd, methodsMap, OBJC_ASSOCIATION_COPY_NONATOMIC);
    return methodsMap;
}

- (NSMutableDictionary *)classExportedMethods:(Class)moduleClass {
    NSMutableDictionary *methodsMap = [NSMutableDictionary dictionary];
    
    unsigned int methodCount;
    Method *methods = class_copyMethodList(object_getClass(moduleClass), &methodCount);
    
   for (unsigned int i = 0; i < methodCount; i++) {
        Method method = methods[i];
        SEL selector = method_getName(method);

        if ([NSStringFromSelector(selector) hasPrefix:@"__fm_export__"]) {
            IMP imp = method_getImplementation(method);
            NSDictionary *methodMap = ((NSDictionary *(*)(id, SEL)) imp)(moduleClass, selector);
            NSAssert(methodMap.count, @"dont export method");
            NSString *exportMethodName = methodMap.allKeys[0];
            NSAssert(exportMethodName.length, @"dont export method name error");
            NSRange range = [exportMethodName rangeOfString:@":"];
            NSString *name = range.location != NSNotFound ? [exportMethodName substringToIndex:range.location] : exportMethodName;
            [methodsMap setValue:methodMap[exportMethodName] forKey:name];
        }
    }
    free(methods);

    if (moduleClass.superclass != [NSObject class]) {
        [methodsMap addEntriesFromDictionary:[self classExportedMethods:moduleClass.superclass]];
    }

    return methodsMap;
}
@end

TuWei1992 avatar May 10 '17 14:05 TuWei1992

根据你的思路写了一种惯用的实现方式:https://github.com/Ftkey/LTJSBridge 非常感谢你的开源代码。

ftkey avatar Jun 13 '18 06:06 ftkey