SoftMemes
SoftMemes

Reputation: 5702

Is it possible to dynamically implement a protocol in Objective-C?

I know that I can extend a class (for example a framework class) using categories, but is it possible to have a class for which you do not control the source code implement one of your custom protocols? I not only want to have it respond to certain messages if sent to an instance, but also, ideally, want objects of that class to return true on runtime type checks when querying for the protocol.

Upvotes: 3

Views: 2625

Answers (2)

user23743
user23743

Reputation:

You can define a category that conforms to the protocol, so you'd do something like:

@interface UIWebView (MyGreatExtensions) <UITableViewDelegate>
@end

@implementation UIWebView (MyGreatExtensions)

- (CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath {
  return 42.0;
}

// ...

@end

A small edge case to consider is that if someone else tries to do the same thing (e.g. a third-party framework also adds the protocol via a category) then you can't guarantee that your version will be used (and of course neither can they).

More on this approach from Mark Dalrymple.

Upvotes: 8

Kristian Duske
Kristian Duske

Reputation: 1779

I think this is possible. You can easily create dynamic proxies in Objective-C (this is how NSUndoManager does its magic) by overwriting NSObject's forwardInvocation: method, intercepting all non-recognized messages that an implementor of the protocol would respond to. I have never done this myself, so there might be a more elegant way to do this.

In addition, to fool the runtime checks into agreeing that your object does implement the protocol, you could override NSObject's class method conformsToProtocol: like so:

+ (BOOL)conformsToProtocol:(Protocol *)aProtocol {
    if (aProtocol == @protocol(MyDynamicallyImplementedProtocol))
        return YES;
    return [super conformsToProtocol:aProtocol];
}

There might be more methods that you need to override, one example would be NSObject's instancesRespondToSelector: and the resolve*: methods. Examine the NSObject class reference to find out more.

Upvotes: 2

Related Questions