班级扩展与班级类别 [英] Class extension vs class category
问题描述
类扩展@interface Class ()
功能强大得多,可以将变量注入到类中.类别@interface Class (Category)
不能.
Class extensions @interface Class ()
are a lot more powerful and can inject variables into the class. Categories @interface Class (Category)
can't.
还有什么其他区别?什么时候应该在类扩展上使用类别?
What other differences are there, and when should one use a category over a class extension?
推荐答案
主要区别在于,通过扩展,编译器将期望您在主@implementation
内实现方法,而对于类别,则拥有单独的类别@implementation
块.因此,您几乎应该只在主.m
文件的顶部使用扩展名(顺便说一句,您应该在这唯一的地方关注ivars)-就是说,是扩展名.
The main difference is that with an extension, the compiler will expect you to implement the methods within your main @implementation
, whereas with a category you have a separate @implementation
block. So you should pretty much only use an extension at the top of your main .m
file (the only place you should care about ivars, incidentally) -- it's meant to be just that, an extension.
这篇关于班级扩展与班级类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!