ios-5-Categories and Protocols

Source: Internet
Author: User

Category: Extending the built-in functionality of existing classes (the ability to extend classes without inheritance)

A category can extend a property that does not exist before the class, and cannot add instance variables to the class interface as inherited, but rather extends the behavior of a class

In addition to adding new behavior to existing classes, categories also support grouping related methods into multiple separate files for classes that you build

The privatization of methods or properties can be implemented with anonymous classes in M files

Add after import:

Interface class name () {

Privatization properties

}

Protocol: A protocol defines a priori way for a class to communicate with another class, which contains a list of methods, either optional or mandatory. The methods defined in the protocol include some operation responses and event responses. Both the delegate and the data source are generated by the protocol.

A protocol is similar to an interface in Java, and defines a set of methods that the classes that follow the agreement agree to satisfy this set of methods list

Protocols can be used to define callback methods (both iOS delegates and data sources are implemented using protocols, Uitableviewdelegate, Uitableviewdatasource)

The protocol is divided into the optional implementation method and the necessary implementation method, which is differentiated by keyword @required and @optional

An optional way to determine whether a class implements the protocol:

[Java]View Plaincopyprint?
    1. if ([self.client respondstoselector: @selector (dosomething)])
    2. {
    3. [Self.client dosomething];
    4. }

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.