ios Objective-C 中的“类别”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7414011/
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-30 21:31:20  来源:igfitidea点击:

What is “category” in Objective-C?

iphoneobjective-cioscategories

提问by wOlVeRiNe

Possible Duplicate:
How does a category work in Objective-C?

可能的重复:
类别在 Objective-C 中是如何工作的?

I am just getting started with Objective-C and was wondering when and where am i supposed to use category in objective C. is it a class or a protocol? An explanatory example would be of great help. Thanks

我刚刚开始使用 Objective-C,想知道我应该在何时何地在 Objective C 中使用类别。它是一个类还是一个协议?一个解释性的例子会很有帮助。谢谢

回答by Jhaliya

A category allows you to add methods to an existing class—even to one for which you do not have the source.

类别允许您向现有类添加方法,即使是您没有源的类。

Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing

类别是一项强大的功能,它允许您扩展现有类的功能而无需子类化

Check the apple doc for the Category in Objective-C

在 Apple 文档中查看Objective-C 中Category

回答by Abizern

There is a section in the Objective-C 2.0 programming Language document about Categories and Extensions.

Objective-C 2.0 编程语言文档中有一个关于类别和扩展的部分。

As you are just getting started it's a good doc to have on hand to look things up.

由于您才刚刚开始,这是一个很好的文档,可以随时查看。

There is also a section on Categoriesin the Cocoa Core Competencies document, which is easier to get started with, and also has a lot of useful information for the beginner.

Cocoa Core Competencies文档中还有一个关于Categories的部分,比较容易上手,也有很多对初学者有用的信息。