Consider the following Objective-C protocol declaration, which requires only class methods:
@protocol TDWMethoding<NSObject>
+ (void)foo;
+ (void)bar;
@end
Assuming I need to return an instance of a Class
which conforms to this protocol from a method, how am I supposed to specify the return type?
- (nullable /*return-type*/)instantiateMethoding {
Class instance = ... // some implementation
if ([instance conformsToProtocol:@protocol(TDWMethoding)]) {
return instance;
}
return nil;
}
There are a number of working options I considered so far in regards to how to express the /*return-type*/
, but each has its own downsides:
Class
- this way it doesn't expose conformance. What kind ofClass
is it? What does it do? Does it conform to the protocol at all?Class<TDWMethoding>
- this looks like a viable solution and even was suggested a few times by other developers (here and here) but I personally find it inconsistent and misleading: when we have a variable of formType<Protocol> *instance
, it commonly means that protocol class methods should be sent to the instance's class ([[instance class] foo]
) not the instance itself ([instance foo]
);id<TDWMethoding>
and returning an instance of the class instead - this is consistent, but it requires me to instantiate the class, which is both redundant and prevents me from hiding the constructors of the utility classes which conforms to the protocol withNS_UNAVAILABLE
macro.
Is there a better semantic to express such a return-type?