Object-c the difference between an informal agreement and a formal agreement

Source: Internet
Author: User

Object-c the difference between an informal agreement and a formal agreement-Braddoris's Column-Blog channel-csdn.net

http://blog.csdn.net/braddoris/article/details/24621161

Object-c the difference between an informal agreement and a formal agreement

Category: iOS 2014-04-28 09:32 1428 people read Comments (0) favorite reports

These two concepts have plagued me for a long time, and have always been like figuring out what the difference is between an informal agreement and a formal agreement, the following is a combination of online information and your own views on the issue.

I. Informal agreements

Obviously the term is relative to the formal agreement. Before interpreting the informal Agreement, refer to paragraph two:

1, in the "Cocoa design mode," The sixth chapter of the category of 6.3.2 to use the category in the informal agreement section, so write:

Informal agreements are typically defined as categories of nsobject. The method specified in the category interface may or may not actually be implemented by the Framework class. Informal agreements are located in a design gray area. Formal agreements are checked by the compiler and represent a guarantee of the ability of the object, but the informal protocol does not make a guarantee----but only gives hints.

2. Apple's Official document Cocoa Core competencies describes the informal protocol in this article:

An informal protocol are a category onNSObject

The informal protocol is the category of the NSObject class (obviously, its subclasses), and all its subclasses implicitly accept the protocol. (category is a linguistic feature of objective-c that allows you to add methods to a class without having to subclass it.) The implementation of a method in an informal protocol is optional, so before calling a method in an informal protocol, you need to check to see if the object class implements it. Before an optional formal protocol approach is introduced in objective-c2.0, the informal protocol is the only way for the foundation and AppKit classes to implement the delegate.

3. What is an informal agreement

It can be seen from the combination of 1 and 22 that the so-called informal agreement is the category , that is, the categories of nsobject or its subcategories, are informal agreements.

4. Categories and Anonymous categories

The wording of the category:

[CPP] view Plaincopy
  1. @interface  nsstring   (CamelCase)    //category   
  2. -(nsstring*) camelcasestring;
  3. @end
The CamelCase here is the category name, which cannot be duplicated as the class name. It is well known that the category can only add methods and cannot increase instance variables, but the category has the following notation:

[CPP] view Plaincopy
  1. @interface  myclass   ()  {  //class extension   
  2. float  value;     
  3. void float
  4. @end

This type of writing is called Anonymous category , also known as class extension , the so-called extension, in fact, for a class to add additional original variables, methods, or composite properties.

the difference between a category and a class extension:

Only methods can be added to the ① category;

② Yes, you're right. class extensions can not only add methods, but also add instance variables (or composition attributes), except that the instance variable defaults to the @private type (the scope can only be in its own class, not subclasses or elsewhere);

The method declared in the ③ class extension is not implemented and the compiler will alert you, but the methods in the category are not implemented by the compiler without any warning. This is because the class extension is added to the class during the compile phase, and the category is added to the class at run time.

The ④ class extension cannot have a separate implementation part (@implementation part) like a category, meaning that the method declared by the class extension must be implemented by the implementation part of the corresponding class.

⑤ the class extension method defined in the. m file is private, and the class extension method defined in the. h file (header file) is public. A class extension is a very good way to declare a private method in a. m file.

Ii. Formal Agreement

Formal agreements are conceptually easier to understand, referring to a list of methods named in @protocol, which, unlike informal protocols, requires a display of adoption protocols.

1. Method type of formal agreement

There are two types of methods for formal agreement declarations:

① @required

The method of this class must be implemented in a class that adheres to the corresponding protocol, otherwise the compiler will alert you (obviously this is a check at compile time, not at runtime)

② @optional

The method of this class is optional in the class that adheres to the corresponding protocol

2. Significance of the existence of formal agreements

Formally, formal agreements are much more formal than informal agreements, and the significance of their existence is:

Formal agreements can split the method definition in the business into a single file, which coincides with the extraction interface in the traditional oo. If you encounter two systems that need to exchange data, you can develop a set of protocols that both parties adhere to, and then add this protocol file to the project in all two of these systems to implement it.

3, the Inheritance of

Formal agreements are like classes, which can be inherited, similar to the same type of inheritance in written form:

[CPP] view Plaincopy
  1. @protocol newprotocal <Protocal>
  2. @end

Object-c the difference between an informal agreement and a formal agreement

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.