class extension、class category、class-continuation category

class extension

  • Objective-C 2.0增加了class extensions用于解决两个问题:
  1. 允许一个对象可以拥有一个私有的interface,且可由编译器验证。
  2. 支持一个公有只读,私有可写的属性。
  • extension更像是匿名的category

 

class category

  • category更倾向于写在独立的文件中,之后这样命名“NSView+CustomAdditions.h”,在对应的.m文件中的block块中实现。所以,cagegory更倾向于用于对class进行功能的分离,用于组织类的功能模块。
  • extension更倾向于在同一个类的.m文件的最上方写,而其实现则放在该类的implementation block中。
  • 如少鸿上次提供的 UIImage+Display.h ,就是对系统的UIImage类的补充

 

Difference between Category and Class Extension?

  • A category is a way to add methods to existing classes. They usually reside in files called "Class+CategoryName.h", like "NSView+CustomAdditions.h" (and .m, of course).
  • A class extension is a category, except for 2 main differences:
    • The category has no name. It is declared like this:
    • @interface SomeClass ()
          - (void) anAdditionalMethod;
      @end
    • The implementation of the extension must be in the main @implementation block of the file.
  • It's quite common to see a class extension at the top of a .m file declaring more methods on the class, that are then implemented below in the main @implementation section of the class. This is a way to declare "pseudo-private" methods (pseudo-private in that they're not really private, just not externally exposed).

 

 

class-continuation category

class-continuation 分类 见《Effective Objective-C 2.0》第27条

What is an Objective-C “class continuation”?

其实 class-continuation 就是 class extensions,叫法不一样而已

 

posted @ 2015-06-28 21:14  mobilefeng  阅读(647)  评论(0编辑  收藏  举报