Interfaces and generics in objective-C

Source: Internet
Author: User
Admit that I am the Title party, because in the obj-C world, there is no official saying like "interface" or "generic. However, there are two concepts close to OBJ-C" Informal protocol (Interface) "And" Protocal) ". Informal agreement Although the keyword 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 class sample, it will always be a sample. H, Code As follows:
 
# Import <Foundation/Foundation. h> @ interface sample: nsobject {}- (void) helloworld; @ end
It indicates that" Should "There is a method named helloworld (Note: what I am talking about here is Should Instead Required ). 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. But it will still be compiled successfully at the end (that is, the compiler closes one eye with one eye, which defaults to this kind of cheating behavior of the sample class)

Note: incomplete implementation of Class "sample". Meaning: The sample class does not fully implement the methods agreed in interface. This isThe protocol in obj-C is different from the interface in C #: in C #, the interface must be implemented. Otherwise, compilation will fail, while obj-C will warn during compilation, it can finally be compiled.Protocal)In fact, the informal protocol (Interface) is simply a new method. It looks more formal and more semantic: the class that requires the Protocol to be used, "must" implement the methods agreed in the Protocol. However, what is entertaining is thatFormalProtocol. When the compiler is compiling, it still only gives a warning if it is not compliant. (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 the following dbquery: Nsobject <IQUERY> It 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 the following important significance: The formal protocol (Protocal) can split the method definition in the business to form a separate file, which is consistent with the extraction interface in the traditional OO. 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 not available in informal protocols (@ interface. (If you do not believe this, you can change IQUERY in nsobject <IQUERY> to the interface definition name of other classes. Compilation is not successful.) In addition, some extensions are made to the official protocol in obj-C 2.0, the methods in the formal protocol can be identified as "must be implemented ( @ Requied ) And optional ( @ Optional ) "Class 2. If the methods in the Protocol are identified as @ optional, the compiler will not give a warning even if the classes using the 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.IDIt 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 the-(void) test :(ID <IQUERY>) OBJ; this indicates that the test method accepts any type of object as the parameter, but this parameter object must implement the interface IQUERY (it can also be said that this parameter object must use the formal protocol IQUERY ), in C #?

Void test (List <IQUERY>OBJ) looks like?

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.