【问题标题】:What's the difference and use of categories and inheritance? [duplicate]类别和继承的区别和用途是什么? [复制]
【发布时间】:2012-11-12 12:53:41
【问题描述】:

可能重复:
Difference between inheritance and Categories in Objective-c

什么时候应该使用子类化,什么时候应该使用类别?

【问题讨论】:

    标签: objective-c inheritance objective-c-category


    【解决方案1】:

    当您想扩展基础所使用的功能时,子类化。

    @interface MyObject: NSObject<SomeProtocol>
    

    当您想为您可能无法控制的代码添加便捷方法时添加一个类别。

    @interface UIView (MyViewAdditions)
    
    - (void)recursiveEnumerateSubviewsUsingBlock:(void (^)(UIView *view, BOOL *stop))block;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-11
      • 2010-10-06
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      相关资源
      最近更新 更多