Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 类别和类扩展之间的区别?
我经常被告知,当你想将一个变量声明为私有时,你会在 .m 文件上创建一个无名类别。
与命名类别相比,无名类别有什么本质上的不同吗?
有什么不同?
您所说的“无名类别”称为“类扩展”。
一个区别是编译器期望类扩展中的声明在类@implementation块中定义。@implementation也就是说,当扩展块与主块处于相同的翻译中时,您不会显式声明扩展@implementation块。为了您的方便,它们被合并了。因此,编译器可能会产生警告。
@implementation
另一个区别是扩展可以声明合成的属性(并因此调整类型的大小),而常规类别则不能。除了属性之外,实例变量也可以在类扩展的@interface.
@interface
除此之外,这只是惯例。