Category for a class that conforms to a protocol [

2019-08-22 03:40发布

问题:

This question already has an answer here:

  • Can a category implement a protocol in Objective C? 1 answer
  • Making class conform to protocol with category for existing methods 2 answers

I'm trying to implement a category for a UIViewController and I want to be certain that the object conforms to a certain protocol. Something like this:

#import <UIKit/UIKit.h>

@interface UIViewController<MyProtocol> (Category)

@end

Is this possible?

回答1:

Swap category and protocol:

@interface UIViewController (Category) <MyProtocol>