在公共接口和私有接口/实现文件上声明协议之间的区别 [英] difference between declaring protocols on public interface and private interface /implementation file

查看:140
本文介绍了在公共接口和私有接口/实现文件上声明协议之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以这些方式宣布这些协议之间的区别是什么?只是.h文件中的那些是公开的?

What is / are the differences between declaring these Protocols these ways? Is it just that the ones in the .h file are publicly available?

in .h file:
@interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

in .m file:
@interface TestViewController () <UISearchBarDelegate, UISearchDisplayDelegate, UIAlertViewDelegate, MKMapViewDelegate, CLLocationManagerDelegate>


推荐答案

将协议添加到.h文件时,这告诉每个人,包括该类遵守给定协议的头文件。

When you add the protocols to the .h file, this tells everyone that includes the header file that the class adheres to the given protocols.

当您将协议添加到.m文件时,这实际上是一个私人指示,该类遵守协议。只有实现才知道。

When you add the protocols to the .m file, this is essentially a private indication that the class adheres to the protocols. Only the implementation knows.

当外部类需要知道类遵守协议时,您应该只使用第一个表单(在.h文件中)。当只有实现关注时,你应该使用第二种形式(在.m文件中)。

You should only use the first form (in the .h file) when outside classes need to know that the class adheres to the protocol. You should use the second form (in the .m file) when only the implementation cares.

在你给出的例子中,其他类不太可能需要知道关于遵守表视图协议的类。那些应该在.m文件中。任何其他类都不太可能需要了解搜索协议。这些是实现细节。这些属于.m文件。

In the example you gave, it is highly unlikely that other classes need to know about the class adhering to the table view protocols. Those should be in the .m file. It's also unlikely any other class needs to know about the search protocols. These are implementation details. These belong in the .m file.

可能存在两种情况都使用的情况。这很好。

There may be cases where you use both. This is fine.

这是我的指南。将它放在.m文件中,除非您特别需要让其他类知道协议的使用。

Here' my guideline. Put it in the .m file unless you have a specific need to let other classes know about the use of the protocol.

这篇关于在公共接口和私有接口/实现文件上声明协议之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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