Protocol and OCprotocol in OC

Source: Internet
Author: User

Protocol and OCprotocol in OC
1. Simple use 1. Basic use

  • Can be used to declare a lot of methods (member variables cannot be declared)
  • As long as a class complies with this Protocol, it is equivalent to having all the method declarations in this Protocol.
  • As long as the parent class complies with a certain protocol, it is equivalent to the sub-class also complies
2. Format
  • Protocol Compilation

@ Protocol name

// Method declaration list

@ End

  • A class complies with the Protocol

@ Interface Class Name: parent class <protocol name>

@ End

3. Keywords

The Protocol has two keywords to control whether a method is implemented (@ required by default). In most cases, it is used for communication between programmers.

  • @ Required: This method must be implemented (if not implemented, the compiler will issue a warning)
  • @ Optional: This method does not have to be implemented
4. Agreement compliance
  • One protocol can comply with multiple other protocols. Multiple protocols are separated by commas (,).
  • An agreement that complies with other protocols is equivalent to having a method statement in other Protocols

@ Protocol name <protocol 1, protocol 2>

@ End

5. Base protocols
  • NSObject is a base class, the most fundamental and basic class. Any other class will eventually inherit it.
  • In fact, there is also a protocol named NSObject, which is a base protocol and the most fundamental and basic protocol.
  • The NSObject Protocol declares many basic methods, such as description, retain, and release.
  • We recommend that each new protocol comply with the NSObject protocol.
6. Specify the protocol when defining variables.

// NSObject-type object, which must comply with the NSCopying Protocol

NSObject <NSCopying> * obj;

// Any OC object and comply with the NSCoding Protocol

Id <NSCoding> obj2;

Ii. Proxy design mode 1. Design Principle
  • If you don't want to do something troublesome, you can ask someone for help, that is, hand it over to the proxy object.
2. Design Principles
  • First, you must have a proxy object property.
  • Next, we need to clearly know the proxy methods.
  • Finally, ensure decoupling.
3. Implementation Scheme
  • Define a protocol and declare some methods to communicate with the proxy.
  • Has a proxy property id <protocol> delegate
  • Allow the proxy to comply with protocol

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.