Protocol in objective-C is the same as the interface in Java and the pure virtual class in C ++. Essentially, protocol is used for a communication protocol to be followed by a class, in this sense,
The objective-C protocol may be more appropriate.
1. How to define protocol?
The definition of protocol is very simple and basically follows the following format:
@ Protocol protocolname
// Here is protocol method list that you must implement
@ End
2. How to Use protocol?
A class must follow (conforms) A protocol, put the protocols to be followed in <>, and separate multiple protocols with commas, as shown below:
@ Interface someclass <Protocol1, Protocol2>
@ End
Does it look a bit like a template class in C ++?
Iii. Other protocols
1. The methods in Protocol do not need to appear in the interface declaration again, because the methods in protocol have been declared, but must be implemented in implementation.
That is to say, the methods required by Protocol does not need to be placed in the methods list of the header file again, but must be placed in the corresponding M file for implementation.
2. The objective-C interface system has a unique concept: How to Develop a category. Unlike C/C ++, printing * somevar = (printing *) someobj,
Instead, you can add protocol to the ID type, for example, id <printing> somevar = someobj. You can also dynamically specify the types that follow multiple protocols,
However, you only need to use one variable from start to end: <printing, nscopying> somevar = someobj;
3. Just like using @ selector to test the object's inheritance relationship, you can use @ protocol to test whether an object complies with a certain protocol. If so, [someobject conformstoprotocol: @ protocol (someprotocol )]
Returns a Yes bool object. The same is true for the class: [someclass conformstoprotocol: @ protocol (someprotocol)]
4. Example: from objective-C _ Simplified Chinese manual
// Define a protocol method.
1, printing. h:
@ Protocol Printing
-(Void) print;
@ End
2, fraction. h:
# Import <Foundation/nsobject. h>
# Import "printing. H"
// You can see the method in Protocol printing: Print does not appear in the method list below
@ Interface fraction: nsobject <printing, nscopying> {
Int numerator;
Int denominator;
}
-(Fraction *) initwithnumerator: (INT) n denominator: (INT) D;
-(Void) setnumerator: (INT) D;
-(Void) setdenominator: (INT) D;
-(Void) setnumerator: (INT) n anddenominator: (INT) D;
-(INT) numerator;
-(INT) denominator;
@ End
3, fraction. m
# Import "fraction. H"
# Import <stdio. h>
@ Implementation Fraction
-(Fraction *) initwithnumerator: (INT) n denominator: (INT) d {
Self = [Super init];
If (Self ){
[Self setnumerator: N anddenominator: d];
}
Return self;
}
// Note: The methods in Protocol need to be implemented here
-(Void) print {
Printf ("% I/% I", numerator, denominator );
}
-(Void) setnumerator: (INT) n {
Numerator = N;
}
-(Void) setdenominator: (INT) d {
Denominator = D;
}
-(Void) setnumerator: (INT) n anddenominator: (INT) d {
Numerator = N;
Denominator = D;
}
-(INT) denominator {
Return denominator;
}
-(INT) numerator {
Return numerator;
}
// Note: This is the method in implementing nscopying protocol.
-(Fraction *) copywithzone: (nszone *) Zone {
Return [[fraction allocwithzone: Zone] initwithnumerator: numerator denominator: denominator];
}
@ End
4, Main. m
# Import <stdio. h>
# Import "fraction. H"
Int main (INT argc, const char * argv []) {
// Create a new instance
Fraction * frac = [[fraction alloc] initwithnumerator: 3 denominator: 10];
// How to use ID and protocol to reference objects
ID <printing> printable;
ID <nscopying, printing> copyprintable;
// Print it
Printable = frac;
Printf ("the fraction is :");
[Printable print];
Printf ("\ n ");
// This compiles because fraction comforms to both printing and nscopyable
Copyprintable = frac;
// True
If ([frac conformstoprotocol: @ protocol (nscopying)] = Yes ){
Printf ("fraction conforms to nscopying \ n ");
}
// Free memory
[Frac release];
Return 0;
}