ios IOS开发中的协议、扩展和类别有什么区别?以及如何恰当地使用它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30859411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:27:08  来源:igfitidea点击:

What is the difference between protocol, extension and category in IOS development? And how to use them appropriately?

iosobjective-cswift

提问by Chris Forever

Also, is there any difference for these 3 things between Swift and Objective-C languages?

另外,Swift 和 Objective-C 语言在这 3 件事上有什么区别吗?

回答by Vivek Molkar

Protocols

协议

A protocol declares a programmatic interface that any class may choose to implement.

协议声明了一个任何类都可以选择实现的编程接口。

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. Protocols are like Interfaces, that provide some methods that the class conforming must implement.

协议定义了适合特定任务或功能的方法、属性和其他要求的蓝图。协议就像接口,它提供了一些符合的类必须实现的方法。

Uses:

用途:

  • A common use case is to let you alter the behavior of certain classes without the need to subclass them.
  • Eg: UITableViewDelegate, UITableViewDataSource
  • 一个常见的用例是让您改变某些类的行为,而无需对它们进行子类化。
  • 例如:UITableViewDelegateUITableViewDataSource

Also see Protocol

另见协议

Extension

延期

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling).

扩展向现有类、结构、枚举或协议类型添加新功能。这包括扩展您无法访问原始源代码的类型的能力(称为追溯建模)。

Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions do not have names.)

扩展类似于 Objective-C 中的类别。(与 Objective-C 类别不同,Swift 扩展没有名称。)

Uses:

用途:

  • Categories are a way to modularize a class by spreading its implementation over many files. Extensions provide similar functionality.

  • One of the most common uses of categories is to add methods to built-in data types like NSStringor NSArray. The advantage of this is that you don't have to update existing code to use a new subclass

  • 类别是一种通过将类的实现扩展到许多文件来模块化类的方法。扩展提供了类似的功能。

  • 类别最常见的用途之一是向内置数据类型(如NSString或 )添加方法NSArray。这样做的好处是您不必更新现有代码即可使用新的子类



Extensions and Categories have some difference in Objective-C

扩展和类别在Objective-C 中有些区别

Note: Following is true only for Objective-C

注意:以下仅适用于 Objective-C

Categories allow you to add methods outside of the main interface file. Whereas Extensions must be implemented in main Interface file. Which means we can safely conclude you cannot use extensions for extending Builtin classes or Classes for which you don't have the source code, there you should use Categories. And to use extensions you need access to the source of the class you are extending.

类别允许您在主界面文件之外添加方法。而扩展必须在主接口文件中实现。这意味着我们可以安全地得出结论,您不能使用扩展来扩展内置类或没有源代码的类,您应该使用类别。并且要使用扩展,您需要访问您正在扩展的类的源代码。

回答by Tushar

Protocol

协议

Extension

延期

Category

类别

The concept of protocol and extension has similarities between Swift & Objective-C. Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions do not have names.)

协议和扩展的概念在 Swift 和 Objective-C 之间有相似之处。扩展类似于 Objective-C 中的类别。(与 Objective-C 类别不同,Swift 扩展没有名称。)