The difference between IOS-class extensions and classifications

Source: Internet
Author: User

Class extension (also known as anonymous classification)
  • Role:
    • Additional attributes, member variables, method declarations can be attached to a class
    • The generic class extension is written to the. m file
    • General private properties are written to class extensions
  • Use format:
    @interface Mitchell()//属性//方法@end
  • The difference from classification

    • The parentheses must have a name in the category

      @interface 类名(分类名字)/*方法声明*/@end@implementation类名(分类名字)/*方法实现*/@end
    • A taxonomy can only extend methods and cannot extend attributes and member variables (if the containing member variable will directly error).
    • If an attribute is declared in the classification, then the classification generates only the set, get method declaration of the property , i.e. no implementation.
    • Example: If we were to add two attributes to the class extension and classification,

      Class extensions with category 1.1.png
      Next, assign the values in the initialization method separately,

      Class extensions with category 1.1.png
      You will see a crash when assigning a value to the attribute Textone declared in the taxonomy, so let's look at the cause of the crash:

      Class extensions with category 1.3.png
      This means that the object we created does not textone this property. In other words, although we declare the attribute in the category will not error 但是@property并没有自动为我们设置的属性生成set、get方法 .
  • And again, why can't we include the. m file for the class, because this repeats the implementation file that contains the other class.

The code for using the runtime is as follows:

  @interface   Qkycrmmycustomercontroller ( mccate) @property (nonatomic,copy) nsstring  *MYSELFSTR;   @end   @implementation   Qkycrmmycustomercontroller (mccate)  static  NSString *const   mystr;     -(void ) Setmyselfstr: (nsstring *) myselfstr{ Objc_setassociatedobject (self,  &mystr, Myselfstr, objc_association_copy) ;} -(NSString *) myselfstr{ return  objc_ Getassociatedobject (self, &mystr);}   @end  

Wen/mitchell (author of Jane's book)
Original link: Http://www.jianshu.com/p/18d48e7f2aad
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

The difference between IOS-class extensions and classifications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.