Category for a class that conforms to a protocol [duplicate]
Asked Answered
P

1

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?

Pilgrim answered 22/5, 2013 at 2:4 Comment(3)
I believe you are asking about something similar to this: #17740062Pyroelectricity
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.Pilgrim
Mods: The two questions 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 (available in Obj-C and Swift 1) vs. extending a protocol (available in Swift 2 only).Pyroelectricity
G
1

Swap category and protocol:

@interface UIViewController (Category) <MyProtocol>
Gyrose answered 22/5, 2013 at 7:38 Comment(11)
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<Protocol> castedSelf = (id<Protocol>)self;Pilgrim
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.Gyrose
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.Pilgrim
Which (formal) protocol is implemented in NSObject without declaring it?Gyrose
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.Pilgrim
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.Gyrose
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.Pilgrim
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.Gyrose
I only need one category to serve as a delegate and that category only needs to satisfy the delegate's protocol.Pilgrim
Sounds like two things …Gyrose
Thanks for your input. I'll post an example when I get the chance.Pilgrim

© 2022 - 2024 — McMap. All rights reserved.