OC tutorial 3-category extension Protocol

Source: Internet
Author: User

OC tutorial 3-category extension Protocol
OC3-language features

This chapter mainly describes the OC language features.

  1. Category)
  2. Extension)
  3. Protocol 1, category

    The category mechanism in the OC language has only one function: Adding Methods to any class

    Now we create a Student class

    @interface Student : NSObject@property(nonatomic,strong)NSString * name;@end@implementation@end

    If you want to add a method for the Student class, you can use either of the following methods:

    1. Directly modify the source code of the Student class, declare a method, and implement it.
    2. Use category.

      When the source code of a class cannot be obtained and a method needs to be added to the class, the function of the class is very important.

      The syntax for creating a category is also very simple. The Code is as follows:

      @interface Student (Test)-(void)test;@end
      @ Implementation Student (Test)-(void) test {NSLog (@ "here is the test method of the Student Class");} @ end

      The declaration and implementation keywords are the same as before.

      The difference is that you do not need to specify the parent class during the declaration, but directly write the name of the class to add the method. However, you must add a bracket and write the category name in the brackets. A class can have multiple categories.

      With this category, although the source code of the Student class does not contain the test method, the Student object can still be used to call this method normally.

      Student * s = [[Student alloc] init];[s test];
      2. Extend

      Extension can be seen as an anonymous category. Classes sometimes need to be seen only by themselves. Private methods used can be declared through extension.

      For example, the Student class in the previous section has a cheating method, which is not viewed by the outside, but does not affect the calls. This method can be declared in the extension, and the implementation of the method is still in the Student class.

      @interface Student ()-(void)zuobi;@end

      Generally, we place the class declaration in a file with the extension. h and the implementation in a file with the extension. m. The. m file of the executable package type in production will be compiled and encrypted into the. a file and irreversible.

      Student. h file

      @interface Student : NSObject@property(nonatomic,strong)NSString * name;@end

      Student. m file

      @ Interface Student ()-(void) zuobi; @ end @ implementation Student (Test)-(void) test {NSLog (@ "here is the test method for the Student class ");} -(void) zuobi {NSLog (@ "cheating");} @ end

      Generally, the extended declaration is stored in the. m file, so that the class method can be hidden and not discovered by the outside world and can be used normally in the class.

      3. Protocol

      The protocol in OC is a declaration of a set of methods. It is not required. The compliance class is responsible for implementing the methods in the Protocol.

      Protocol usage@protocolKeyword.

      The declared methods include required methods and optional methods. Use@requiredAnd@optionalKeyword.

      If this parameter is left blank, the default method is required.

      @ Protocol Test
            
             
      @ Required-(void) fun1; // class that complies with this Protocol, required method @ optional-(void) fun2; // class that complies with this Protocol, optional implementation method @ end
            

      Making an agreement is simple. We can define the behavior of an object through the agreement.

      For example, the following method

      -(void)test:(id
            
             )obj;
            

      This method requires,objThe object must be an object created by a class that complies with the Test protocol.

      @property id 
            
             obj;
            

      Likewise, attribute objects can be defined using protocols.

Related Article

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.