0

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?

  • I believe you are asking about something similar to this: http://stackoverflow.com/questions/17739562/creating-a-category-for-classes-that-implement-a-protocol-in-objective-c – Senseful Jul 19 '13 at 06:43
  • Swift's Protocol Extensions are what I was thinking of. Protocol extensions add behavior to a type that conform to the protocol vs. fulfilling a protocol's expectations with an extension. – Brian Semiglia Nov 11 '15 at 01:35
  • 1
    Mods: The [two](http://stackoverflow.com/questions/5824755) [questions](http://stackoverflow.com/questions/7255392) marked as duplicates are completely different than what @Brian was asking for; he was asking about extending a protocol. There is a significant difference between **[extending a class to conform to a protocol](http://stackoverflow.com/questions/5824755)** (available in Obj-C and Swift 1) vs. **[extending a protocol](http://stackoverflow.com/questions/17739562)** (available in Swift 2 only). – Senseful Nov 11 '15 at 18:55

1 Answers1

1

Swap category and protocol:

@interface UIViewController (Category) <MyProtocol>
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • I don't want the category to implement the protocol. I want to make sure the category's base class does. I solved this by casting self into a temporary pointer and declaring it's protocol there. id castedSelf = (id)self; – Brian Semiglia Jul 19 '13 at 14:40
  • The casting makes nothing sure. A category knows its base class. It is not a mixin. So the baseclass can simply implement the protocol, declare that on an interface and the category can know about it. – Amin Negm-Awad Jul 24 '13 at 12:31
  • The cast is necessary in sending messages to properties/methods of a base class that implements a protocol under ARC. If my base class is NSObject and implements a protocol, omitting a protocol declaration would leave me with a mostly useless base class. And yes, the casting makes nothing sure. That was the problem I was originally hoping to solve. – Brian Semiglia Jul 30 '13 at 14:15
  • Which (formal) protocol is implemented in NSObject without declaring it? – Amin Negm-Awad Jul 30 '13 at 14:21
  • The protocol is not formal. It's my own. I am creating a category on NSObject that can act as a delegate for another type of object I've created. Using a category allows for portability. Using a protocol allows the category to manipulate the base object in response to delegate callbacks without needing to know what kind of object it is. – Brian Semiglia Jul 30 '13 at 15:21
  • Whether a protocol is formal or not does not depend on who defined it?! if you want to implement a protocol on an existing class using a category, simply declare ist as i wrote in my answer. – Amin Negm-Awad Jul 30 '13 at 17:29
  • The category is implementing the delegate protocol but it is also manipulating the base object within those callbacs and can only do so if the base object conforms to the additional protocol. For example: An object calls out to it's delegate, the delegate methods are forwarded to the category, and the those category methods manipulate it's base NSObject with messages that go beyond what is declared in the NSObject interface. – Brian Semiglia Jul 30 '13 at 18:14
  • It is possible to have more than one category on a class. And it is possible to have more than one protocol in a category's interface. – Amin Negm-Awad Jul 30 '13 at 18:56
  • I only need one category to serve as a delegate and that category only needs to satisfy the delegate's protocol. – Brian Semiglia Jul 30 '13 at 22:24
  • Sounds like two things … – Amin Negm-Awad Jul 31 '13 at 04:43
  • Thanks for your input. I'll post an example when I get the chance. – Brian Semiglia Jul 31 '13 at 15:24