獲取某個 protocol 協議下的所有方法

- (NSArray<NSString *> *)appDelegateMethods

{

    static NSMutableArray *methods = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        unsigned int methodCount = 0;

        // 獲取UIApplicationDelegate協議所有協議方法

        struct objc_method_description *methodList = protocol_copyMethodDescriptionList(@protocol(UIApplicationDelegate), NO, YES, &methodCount);

        methods = [NSMutableArray arrayWithCapacity:methodCount];

        for (int i = 0; i < methodCount; i ++)

        {

            struct objc_method_description md = methodList[i];

            [methods addObject:NSStringFromSelector(md.name)];

        }

        free(methodList);

    });

    // 返回協議方法數組

    return methods;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章