ios - 为什么我们需要在IOS中使用category?

标签 ios objective-c objective-c-category

我刚刚开始在 ios 中开发应用程序,我已经阅读了很多教程,但我仍然不满意该教程。 我想知道理论上为什么我们需要使用类别以及有什么好处。

最佳答案

引用自

http://www.g8production.com/post/37787310116/categories-in-objective-c-how-to-extend-methods

Difference between Category and Class Extension?

类别 和扩展允许您扩展现有类的功能,而无需子类化(不继承任何内容)向现有类添加功能,即使是您不希望添加的功能有来源。

类别允许您通过在接口(interface)文件 (.h) 中声明它们并在实现文件 (.m) 中定义它们来向类添加(仅)方法,就像在基本的 Objective-C 类中一样。遗憾的是,类别不能为类声明额外的实例变量。

现在这个声明的方法成为分类类的一部分!!!

可以添加到分类类中的类别数量没有限制,但每个类别名称必须不同,应声明和定义一组不同的方法。

编辑

类别

-> In objective c, when you want to add some more functionality to a class without inheritance, you simply use category for it.

-> Category use to add new method not properties

.

类扩展

-> In objective c, when you want to make behavior of some property private you use class extension.

->mainly for properties.

关于ios - 为什么我们需要在IOS中使用category?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32249381/

相关文章:

objective-c - 类别中重写的方法是否始终优先于原始实现?

ios - 如何在swift项目中使用objective-c类别cocoapods

android - 如何在 Cordova 平台中实现推送通知?

ios - 如何在 swift 中将 Objc c Enum 转换为 NSValue

ios - iOS 中同一 1pt 线上的不同颜色

ios - 在不使用 NavigationController 的情况下将导航栏添加到 UITableViewController

ios - UITextField文本在自定义单元格中重复

ios - 两个类别之间的优先级

iOS swift : Displaying asynchronous data on a TableView

ios - 调用 removeFromSuperView() 后 UITableView 仍然存在