类别如何运作? [英] How does a category work?
问题描述
我是Objective-c的新手,需要使用实例变量和访问器来扩展框架的标准类.我听说这是通过所谓的类别"完成的,这对我来说听起来很混乱.这基本上是如何工作的?
I'm new to objective-c and need to extend a standard class of a framework with an instance variable plus accessors. I heard that this is done with a so called "category", which sounds pretty confusing to me. How does this basically work?
推荐答案
一个类别将方法添加到类内部的方法表中.将应用程序特定的方法添加到现有框架类中非常方便.
A category adds methods to the table of methods inside a class. It's very handy for adding application specific methods to existing framework classes.
如果您需要将实例变量添加到类中,则类别将无法完成任务-类别仅添加方法,而不添加数据.要添加实例变量,必须子类化.
If you need to add instance variables to a class, a category won't do the job -- categories only add methods, not data. To add instance variables, you must subclass.
这篇关于类别如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!