Adding the same category to multiple classes

后端 未结 6 610
陌清茗
陌清茗 2021-02-07 03:25

I have an Objective-C category that I\'d like to add to multiple classes without duplicating the code contained in the category. I simply want to add the same methods to multipl

6条回答
  •  别那么骄傲
    2021-02-07 04:21

    maybe it's too late.. But maybe there is one way to do it.. But, you said.. needs to have the same superclass

    Category.h

    @protocol MyProtocol 
    - (NSString*)foo;
    @end
    
    @interface NSArray  (category)  @end
    @interface NSString (category)  @end
    

    Category.m

    @interface NSObject (category)  @end
    @implementation NSObject (category)
    - (NSString*)foo
    {
        return @"bar";
    }
    @end
    

    I don't like this neither, but it works

提交回复
热议问题