ColinE
ColinE

Reputation: 70142

Objective-C proxy for protocol Xcode compiler warnings

I have a class which acts as a generic proxy for forwarding delegates. By implementing respondsToSelector and forwardInvocation I am able to use this class to forward selectors that would be sent to a delegate for various UI controls.

The problem is that when I use this class I get a compiler warning. For example, if I use it to forward messages to the UITableViewDelegate:

DelegateForwarder* _forward = [[DelegateForwarder alloc] init];
_tableView.delegate = _forward;

I get the compiler warning:

Assigning to id<UITableViewDelegate> from incompatible type 'DelegateForwarder'

I know that the warning is generated because DelegateForwarder does not adopt the UITableViewDelegate delegate, but I do not want it to - because the forwarding mechanism it provides is entirely generic.

Despite the warning my code runs as intended.

Is there anything I can do to indicate in some way to the compiler and users of this class that it uses message forwarding, so the fact that it does not adopt a specific protocol is not an issue?

Upvotes: 0

Views: 244

Answers (2)

kovpas
kovpas

Reputation: 9593

Check if you added <UITableViewDelegate> to a list of DelegateForwarder's protocols. In general it's more proper way than initializing with generic "id" type.

Upvotes: 0

Phillip Mills
Phillip Mills

Reputation: 31016

Expanding on the comments....

Since id is called a "generic object type", it's use is a signal to the type-checking mechanism that the nature of the referenced object is to be considered completely dynamic. This means that code such as...

DelegateForwarder* _forward = [[DelegateForwarder alloc] init];
_tableView.delegate = _forward;

...implies that the DelegateForwarder variable should be checked for conformance, whereas...

id _forward = [[DelegateForwarder alloc] init];
_tableView.delegate = _forward;

...is a "trust me" type of declaration.

Upvotes: 3

Related Questions