Difference between Category and Class Extension?

后端 未结 9 627
离开以前
离开以前 2020-12-02 07:41

What is the difference between a Category and a Class Extension. I believe both are used to add custom methods in existing classes. Can someone throw light on this? Examplif

9条回答
  •  清歌不尽
    2020-12-02 08:13

    @interface SomeClass ()
    
    
    - (void) anAdditionalMethod;
    
    
    @end
    

    I think it is not the way to declare Category. Category must have a name

    @interface SomeClass (XYZ)
    
    
    - (void) anAdditionalMethod;
    
    
    @end
    

    for example

    @interface NSMutableArray (NSMutableArrayCreation)
    
    + (id)arrayWithCapacity:(NSUInteger)numItems;
    - (id)initWithCapacity:(NSUInteger)numItems;
    
    @end
    

    Declared for NSMutableArray by Apple

提交回复
热议问题