我刚刚开始在 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/