Syntax for multiple protocols
What i开发者_JAVA技巧s the Objective-C syntax for multiple protocols?
Could you please elaborate on your question? Otherwise this is the proper way to declare a class that conforms to multiple protocols. You specify the protocols a class conforms to after the superclass declaration in a classes header file.
@interface MyClass : MySuperClass <Delegate1, Delegate2, Delegate3> {
//instance variables
}
//properties
//methods
You can achieve multiple protocols in two ways:
Method 1
@protocol p1 <NSObject>
-(void)M1
-(void)M2
@end
@protocol p2 <NSObject>
-(void)M3
-(void)M4
@end
@interface MyViewController () <p1,p2>
Method 2
@protocol p1 <NSObject>
-(void)M1
-(void)M2
@end
@protocol p2 <NSObject,p1>
-(void)M3
-(void)M4
@end
@interface MyViewController () <p1>
For an object to have multiple delegates (as opposed to being a delegate for multiple objects or classes):
The object delegating would have to have an NSArray of delegate instance variables.
The setDelegate setter method would then have to add a delegate object to this array instead of just assigning it to one instance variable.
The send-to-delegate code would have to loop through the delegate NSArray, instead of checking for just one delegate instance variable to be non-nil, before checking for message handling and calling out with the message.
Nothing much would change in all the objects or the class asking for delegation to itself.
Yes, I guess the question of comfort to multi protocols is something like the following:
@interface MyViewController () <protocol1, protocol2, protocol3>
精彩评论