码迷,mamicode.com
首页 > 其他好文 > 详细

class extension、class category、class-continuation category

时间:2015-06-28 21:34:41      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

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条

 

class extension、class category、class-continuation category

标签:

原文地址:http://www.cnblogs.com/mobilefeng/p/4606117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!