在objective-c中,无名类和正常类的区别是什么?(复制)

时间:2021-04-16 22:30:21

Possible Duplicate:
Difference between Category and Class Extension?

可能重复:类别和类扩展之间的区别?

I am often told that when you want to declare a variable as private you would make a nameless category on the .m file.

我经常被告知,当您想将一个变量声明为private时,您将在.m文件中创建一个无名类别。

Is there anything inherently different in the nameless category as opposed to named categories?

在无名类别中有什么本质上的不同而不是命名类别?

What's the difference?

有什么区别呢?

1 个解决方案

#1


6  

What you refer to as a 'nameless category' is called a 'class extension'.

你所说的“无名类”被称为“类扩展”。

One difference is that the compiler expects that declarations in the class extension are defined in the class' @implementation block. That is, you do not declare the extension's @implementation block explicitly when it is in the same translation as the primary @implementation block. They are merged for your convenience. Consequently, the compiler may produce warnings.

一个不同之处是,编译器期望类扩展中的声明是在类' @implementation块中定义的。也就是说,当扩展的@implementation块与主@implementation块处于相同的转换时,不要显式地声明它的@implementation块。为了您的方便,它们被合并了。因此,编译器可能会产生警告。

Another difference is that an extension may declare properties which are synthesized (and consequently resize the type), where a regular category cannot. In addition to properties, instance variables may be declared in the class extension's @interface.

另一个不同之处在于,扩展可以声明被合成的属性(从而调整类型的大小),而常规类别不能。除了属性之外,实例变量还可以在类扩展的@interface中声明。

Beyond that, it's just convention.

除此之外,这只是惯例。

#1


6  

What you refer to as a 'nameless category' is called a 'class extension'.

你所说的“无名类”被称为“类扩展”。

One difference is that the compiler expects that declarations in the class extension are defined in the class' @implementation block. That is, you do not declare the extension's @implementation block explicitly when it is in the same translation as the primary @implementation block. They are merged for your convenience. Consequently, the compiler may produce warnings.

一个不同之处是,编译器期望类扩展中的声明是在类' @implementation块中定义的。也就是说,当扩展的@implementation块与主@implementation块处于相同的转换时,不要显式地声明它的@implementation块。为了您的方便,它们被合并了。因此,编译器可能会产生警告。

Another difference is that an extension may declare properties which are synthesized (and consequently resize the type), where a regular category cannot. In addition to properties, instance variables may be declared in the class extension's @interface.

另一个不同之处在于,扩展可以声明被合成的属性(从而调整类型的大小),而常规类别不能。除了属性之外,实例变量还可以在类扩展的@interface中声明。

Beyond that, it's just convention.

除此之外,这只是惯例。