在目标c中使用类别私下扩展协议 [英] extend a protocol privately using a category in objective c

查看:108
本文介绍了在目标c中使用类别私下扩展协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做了一些搜索,没有发现太多,但是想知道是否可以扩展协议。

did some searching and didn't find much, but wondering if it's possible to extend a protocol.

目前,在MyDelegate.h中有: p>

Currently, in MyDelegate.h, there is:

@protocol MyDelegate <NSObject>

希望创建一个私有头,MyDelegate + Private.h,不要将额外的可选方法暴露给客户端。

hoping to create a private header, MyDelegate+Private.h, to not expose extra optional methods to the client.

@protocol MyDelegate (Private) <NSObject>

这似乎不起作用。可能吗?想避免创建一个全新的委托协议,保存当前的一个超集。

this doesn't seem to work. is it possible? would like to avoid creating a whole new delegate protocol that holds a superset of the current one.

推荐答案

我不认为你可以在协议上创建一个类别,但是您可以使用协议创建协议并将其保留在私有头中。苹果在其框架中经常这样做。

I don't think you can create a category on a protocol, but you can create a protocol using a protocol and keep it in a private header. Apple has done this quite frequently in its frameworks.

@protocol MyDelegate_Private <MyDelegate>
- (void)myRequiredPrivateMethod;
@optional
- (void)myOptionalPrivateMethod;
@end

然后在声明类时指定此协议。为了隐藏有一个私有协议的事实,请使用一个公共头,它将该类声明为实现公共协议,并将一个私有头声明为实现私有协议。

Then you specify this protocol when declaring the class. To hide the fact that there is a private protocol, use a public header which declares the class as implementing the public protocol and a private header which declares it as implementing the private protocol.

此外,如果您只在协议中使用可选方法,则不需要制定协议。它所做的只是指定可以实现的方法的列表,并且因为它仅被私有地使用是不必要的。你可以说这个类实现了公共协议,刚刚发生也实现了这些不包含在接口中的其他方法。

Also, if you only use optional methods in the protocol, you don't really need to make a protocol. All it does is specify the list of methods which could be implemented, and since it is only used privately that isn't necessary. You could just say the class implements the public protocol and "just happens" to also implement these other methods which aren't included in the interface.

这篇关于在目标c中使用类别私下扩展协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆