"Informal Protocol" and "formal protocol (protocal)" in ObjC (Objective-C )"

Source: Internet
Author: User
Although the keyword of the informal protocol in obj-c is also interface, this is not exactly the same as the interface in c.

Recall what we have learned before. When we define a Sample class, we always convert it into a Sample. h. The Code is as follows:
#import <Foundation/Foundation.h>@interface Sample : NSObject {}-(void) HelloWorld;@end

It indicates that there is a method named HelloWorld in the Sample class that stipulates "should" (Note: what I am talking about here is yes, not a must). It is just a gentleman agreement.

If we do not comply with this convention in Sample. m (that is, do not implement this method), xcode will give a warning during compilation, as shown in. However, it will still be compiled successfully (that is, the compiler closes one eye for this, by default the Sample class's cheating behavior) in the prompt: Incomplete implementation of class "Sample ". the Sample class does not fully implement the methods agreed in the interface. This is where the protocol in obj-c is different from the interface in c #: the interface in c # is mandatory and must be implemented; otherwise, compilation will fail, while obj-c will warn during compilation, it can finally be compiled. The formal protocol (protocal) is actually an informal protocol (interface). It looks more formal and more semantic: classes that require the protocol, "must" implement the methods agreed in the Protocol. However, it is entertaining that, even if it is known as a formal protocol, the compiler still gives a warning when it encounters noncompliance during compilation. (Of course, the formal protocol also has its meaning, which will be mentioned later) Here we define an IQuery protocol IQuery. h.

@protocol IQuery-(void) Query:(NSString*) sql;@end

In addition to changing the keyword @ interface to @ protocal, the others are basically unchanged. The following defines a class DBQuery and uses this formal protocol DBQuery. h

#import <Foundation/Foundation.h>#import "IQuery.h"@interface DBQuery : NSObject<IQuery> {}@end

Note that DBQuery: NSObject <IQuery> indicates that DBQuery inherits from NSObject and implements the IQuery interface. DBQuery. m

#import "DBQuery.h"@implementation DBQuery-(void) Query:(NSString *)sql{NSLog(@"Query is called. sql:%@",sql);}@end

Of course, if you do not implement the method Query in DBQuery. m, you can compile and pass the Query, but you will receive a warning. So far, you may think that protocal and interface are similar concepts. protocal design is purely redundant. In fact, protocal has an important significance: the formal protocol (protocal) can separate the definition of methods in the business to form a separate file, this is consistent with the extraction interface in traditional OO. For example
If two systems need to exchange data, you can develop a protocal that both parties comply with, and then add the protocol file to the project in both systems to implement it. This function is informal.
The Protocol (@ interface) cannot be implemented. (If you do not believe this, you can change IQuery in NSObject <IQuery> to interfaces of other classes.
Try to define the name and compile it.) In addition, some extensions are made to the formal protocol in obj-C 2.0, allowing the methods in the formal protocol to be marked as "required (@ requied) "and" optional implementations (@ optional) ". If the methods in the Protocol are identified as @ optional, the compiler will not give a warning even if the classes using this Protocol do not implement these methods. This gives formal agreements more flexibility. Example:

@protocol IQuery@required-(void) Query:(NSString*) sql;@optional-(void) HelloWorld; @end

With the @ optional keyword, "informal protocols" can be replaced by "formal protocols" in terms of semantics, in fact, informal protocols in Cocoa are gradually replaced by formal protocols marked with @ optional.

If you select NSObject in XCode code and right-click --> Jump to Definition, you will find that NSObject is actually an interface or protocal.

Select protocal NSObject to continue. The NSObject. h file defines protocal NSObject.

Similarly, you can also see the definition of interface NSObject.

From this point, we can see that the interface NSObject of the informal protocol actually uses the formal protocol protocal NSObject.

That is to say, in the OO world of obj-c, NSObject, the ancestor of all things, is actually a "formal protocol", so all classes derived from NSObject, only one or more protocols are observed.

Another topic "generic"

In obj-c, everything is a pointer. In the previous study, we have come into contact with a special type id, which can be considered as a special pointer: it can point to any type of object. Id, coupled with the formal protocol, can achieve the effect of the c # Generic Type (Note: It is just like it, not like it)

#import <Foundation/Foundation.h>#import "IQuery.h"@interface DBQuery : NSObject<IQuery> {}-(void) test:(id<IQuery>) obj;@end

Note that-(void) test :( id <IQuery>) obj; this indicates that the test method accepts any type of object as a parameter, however, this parameter object must implement the interface IQuery (it can also be said that this parameter object must use the formal protocol IQuery ).

Void test (List <IQuery> obj) looks like?

Author: Yang Guo under the bodhi tree
Source: http://www.cnblogs.com/yjmyzz/archive/2011/03/02/1969126.html
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.
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.