Objective-C protocol of Dynamic Features

Source: Internet
Author: User

In Object-C, Protocol is used to define a group of attributes and Methods independent of a specific class. It does not implement these methods. Any class can implement Protocol, protocol can inherit another Protocol. In terms of features, Protocol functions similar to interfaces in the Java World.

Protocol basically adopts the "Delegation" mode in the specific use process, that is, the Protocol definition method is used, and then different classes use different implementations to complete the polymorphism of a behavior. This Delegate mode is widely used in iOS SDK class libraries. For example, UITableViewDataSource defines a series of methods for using the data source in Table View. When developers need to use Table View, you need to implement this series of methods, and then give the defined Table View delegate to the current object. In this way, Table views on different interfaces can be displayed based on their own object implementations.

Defining protocol is very simple, as follows:

Object-c code

 
 
  1. @protocol MyProtocol <NSObject>  
  2. // pst of methods and properties  
  3. [url=home.php?mod=space&uid=10695]@END[/url]  

In addition, protocol also supports two keywords: @ required and @ optional. As the name implies: all methods after @ required must implement the protocol class, and the methods after optional can not. By default, all methods and attributes are @ required.

Object-c code

 
 
  1. @protocol MyProtocol <NSObject> 
  2. // pst of methods and properties 
  3. @required 
  4. // pst of methods and properties 
  5. @optional 
  6. // pst of methods and properties 
  7. @end 

In addition, protocol can inherit another protocol.

Object-c code

 
 
  1. @protocol A 
  2. -(void)methodA; 
  3. @end 
  4. @protocol B <A> 
  5. -(void)methodB; 
  6. @end 

Protocol implementation is also very simple. In Object-C, if a class uses a protocol, you only need to add '<ProtocolName,...>' to the class definition.

Object-c code

 
 
  1. @interface MyClass : NSObject <MyProtocol, AnotherProtocol, YetAnotherProtocol> 
  2. ... 
  3. <A href="mailto:.@end">@end</A> 

Finally, like interfaces in Java, once an object implements a protocol, you can use the protocol pointer to point to the object. This is why when the current object implements UITableViewDataSource protocol, you can assign the delegate value of Table View to the current object. In the UITableView code, the dataSource attribute definition looks like this.

 
 
  1. Object-c code
  2. ....
  3. Id <UITableViewDataSource> _ dataSource;
  4. & Helpp;

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.