Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Protocol Inheritance in Objective-C

I'll appreciate if anyone can explain the logic behind protocol inheritance. e.g. what does the following mean (UITableView.h):

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> 

The following class implementation doesn't work. I have a class View1 (which inherits UIView), with an associated protocol. I have another class, View2 (which inhertits View1). Now i want to inherit the the protocol as well. Can anyone please point me in the right direction.

Class 1:

@protocol View1Delegate;

@interface View1 : UIView {
    id <View1Delegate> delegate;
    // . . .
}

@property (nonatomic, assign) id <View1Delegate> delegate; // default nil. weak reference

@end

@protocol View1Delegate <NSObject>
- (void)View1DelegateMethod;
@end

@implementation View1

@synthesize delegate;

// . . .
@end

Class 2:

@protocol View2Delegate;

@interface View2 : View1 {
    id <View2Delegate> delegate;
    // . . .
}

@property (nonatomic, assign) id <View2Delegate> delegate; // default nil. weak reference

@end

@protocol View2Delegate <NSObject>
- (void)View2DelegateMethod;
@end

@implementation View2

@synthesize delegate;

// . . .
@end
like image 467
Mustafa Avatar asked Sep 20 '10 13:09

Mustafa


People also ask

What is inheritance in Objective-C?

Inheritance allows us to define a class in terms of another class which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

What is protocol in Objective-C with example?

Protocols are implemented in the classes conforming to the protocol. A simple example would be a network URL handling class, it will have a protocol with methods like processCompleted delegate method that intimates the calling class once the network URL fetching operation is over. A syntax of protocol is shown below.

How do you conform to a protocol in Objective-C?

Objective-C Language Protocols Conforming to Protocols It is also possible for a class to conform to multiple protocols, by separating them with comma. Like when conforming to a single protocol, the class must implement each required method of each protocols, and each optional method you choose to implement.

What is protocol inheritance?

One protocol can inherit from another in a process known as protocol inheritance. Unlike with classes, you can inherit from multiple protocols at the same time before you add your own customizations on top.


1 Answers

Think of it more as composition rather than inheritance.

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> defines a protocol that includes all the methods of the NSObject protocol, the UIScrollViewDelegate protocol, as well as any methods defined for the UITableViewDelegate protocol. When you subclass and create a new property, you're overriding the type of the superclasses property. To make this work how I think you want, you should declare View2Delegate as @protocol View2Delegate <NSObject, View1Delegate>.

like image 81
Joshua Weinberg Avatar answered Sep 22 '22 08:09

Joshua Weinberg