Objective-c 13 Protocol Protocol

Source: Internet
Author: User

The protocol @protocol in Objective-c, similar to the interface interface in Java, lists the methods that this class does not need to implement, and is implemented by classes that implement this Protocol.

1 Declaration Agreement Protocol

Format:

@protocol protocol Name

.... Method

@end

Two methods:

1 is declared directly in a class. For example, the protocol NSObject is declared in the NSObject.h file.

@protocol nsobject-(BOOL) IsEqual: (ID) object, @property (readonly) Nsuinteger hash; @property (readonly) Class Superclass;-(Class) class objc_swift_unavailable ("Use ' anobject.dynamictype ' instead");-(Instancetype) self;-(ID) Performselector: (SEL) aselector;-(ID) performselector: (SEL) Aselector Withobject: (ID) object;-(ID) Performselector: ( SEL) Aselector Withobject: (ID) object1 withobject: (ID) object2;-(BOOL) isproxy;-(BOOL) Iskindofclass: (Class) aclass;-( BOOL) Ismemberofclass: (Class) aclass;-(BOOL) Conformstoprotocol: (Protocol *) aprotocol;-(BOOL) Respondstoselector: ( SEL) aselector;-(instancetype) retain objc_arc_unavailable;-(OneWay void) Release objc_arc_unavailable;-( Instancetype) autorelease objc_arc_unavailable;-(Nsuinteger) retaincount objc_arc_unavailable;-(struct _NSZone *) Zone objc_arc_unavailable; @property (readonly, copy) NSString *description; @optional @property (readonly, copy) NSString *debugdescription; @end
2 Create a separate class declaration


Generate a MyTestProtocol.h file.

#import <Foundation/Foundation.h> @protocol mytestprotocol <NSObject> @required-(void) myTest; @optional- (void) log;-(void) print: (int) value; @end
@required represents the method that the class that implements this Protocol must implement, by default (if @required is not added by default).

@optional means that a class that implements this protocol may or may not be implemented, and is not required.


2 Implementation Protocol

Lists the name of the Protocol after the declaration of the class, enclosed in angle brackets, separated by commas between multiple protocols.

People.h

#import <Foundation/Foundation.h> #import "MyTestProtocol.h" @interface People:nsobject<mytestprotocol, Nscopying> @end
People.m

#import "People.h" @implementation the method that people//must implement-(void) mytest{    NSLog (@ "myTest..");} -(ID) Copywithzone: (Nszone *) zone{    //...    return [[People alloc] init];} Choose the method of implementation-(void) log{    NSLog (@ "Log can choose the method of implementation");} @end
If the method that must be implemented (MyTest, Copywithzone:) is not implemented, a warning is reported.
3 Testing
People *pe = [[People alloc] init]; [PE myTest]; [PE log];//whether the MYTESTPROTOCOL protocol is implemented.] BOOL b = [PE Conformstoprotocol: @protocol (Mytestprotocol)]; NSLog (@ "b=%d", b);
Output:

2016-08-15 11:04:36.282 command line works [1177:49836] mytest. 2016-08-15 11:04:36.282 command Line project [1177:49836] Log selectable method 2016-08-15 11:04:36.282 command-line project [1177:49836] B=1

4 Parent Agreement

The protocol can also inherit the parent protocol, which is similar to inheriting the parent class. The name of the parent agreement can be specified within the angle brackets following the declaration statement protocol name.

@protocol Mytestprotocol <myparenttestprotocol>


@end

The class that implements Mytestprotocol must also implement the methods that must be implemented in its parent protocol. You can usually use NSObject as the root protocol. This is not to be confused with the NSObject class, the NSObject class conforms to the NSObject protocol, which means that all objects conform to the NSObject protocol.





Objective-c 13 Protocol 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.