类扩展与类类别

发布于 2024-11-30 15:14:09 字数 139 浏览 1 评论 0原文

类扩展@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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迎风吟唱 2024-12-07 15:14:09

主要区别在于,对于扩展,编译器会期望您在主 @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.

じее 2024-12-07 15:14:09

类扩展与类别有一些相似之处,但它只能添加到在编译时拥有源代码的类(该类与类扩展同时编译)。类扩展声明的方法是在原始类的 @implementation 块中实现的,因此您不能在框架类上声明类扩展,例如 Cocoa 或 Cocoa Touch 类(如 NSString)。

声明类扩展的语法与类别的语法类似,如下所示:

@interface ClassName ()
@end

由于括号中没有给出名称,因此类扩展通常称为匿名类别。

与常规类别不同,类扩展可以将自己的属性和实例变量添加到类中。如果您在类扩展中声明属性,如下所示:

@interface XYZAnimal () {
    id _someCustomInstanceVariable;
}
...
@end

恕我直言,最好将类扩展视为类的私有接口。主接口(在 .h 文件中)充当公共接口,定义该类与其他类的行为契约。

使用类扩展来隐藏私有信息

类扩展通常用于通过在类本身的实现中使用的附加私有方法或属性来扩展公共接口。例如,通常在接口中将属性定义为只读,但在实现上方声明的类扩展中将属性定义为可读写,以便类的内部方法可以直接更改属性值。

例如,XYZPerson 类可能会添加一个名为 uniqueIdentifier 的属性,旨在跟踪美国社会安全号码等信息。

通常需要大量的文书工作才能为现实世界中的个人分配唯一的标识符,因此 XYZPerson 类接口可能会将此属性声明为只读,并提供一些请求分配标识符的方法,如下所示

@interface XYZPerson : NSObject
    ...
    @property (readonly) NSString *uniqueIdentifier;
    - (void)assignUniqueIdentifier;
@end

:为了使 XYZPerson 类能够在内部更改属性,在该类的实现文件顶部定义的类扩展中重新声明该属性是有意义的:

@property (readwrite) NSString *uniqueIdentifier;

注意:readwrite 属性是可选的,因为它是 默认。为了清楚起见,您可能希望在重新声明属性时使用它。

A class extension bears some similarity to a category, but it can only be added to a class for which you have the source code at compile time (the class is compiled at the same time as the class extension). The methods declared by a class extension are implemented in the @implementation block for the original class so you can’t, for example, declare a class extension on a framework class, such as a Cocoa or Cocoa Touch class like NSString.

The syntax to declare a class extension is similar to the syntax for a category, and looks like this:

@interface ClassName ()
@end

Because no name is given in the parentheses, class extensions are often referred to as anonymous categories.

Unlike regular categories, a class extension can add its own properties and instance variables to a class. If you declare a property in a class extension, like this:

@interface XYZAnimal () {
    id _someCustomInstanceVariable;
}
...
@end

IMHO, it's best to think of class extensions as private interface to a class. The primary interface (in your .h file) acts as the public interface which defines the class's behavioural contract with other classes.

Use class extensions to Hide Private Information

Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class itself. It’s common, for example, to define a property as readonly in the interface, but as readwrite in a class extension declared above the implementation, in order that the internal methods of the class can change the property value directly.

As an example, the XYZPerson class might add a property called uniqueIdentifier, designed to keep track of information like a Social Security Number in the US.

It usually requires a large amount of paperwork to have a unique identifier assigned to an individual in the real world, so the XYZPerson class interface might declare this property as readonly, and provide some method that requests an identifier be assigned, like this:

@interface XYZPerson : NSObject
    ...
    @property (readonly) NSString *uniqueIdentifier;
    - (void)assignUniqueIdentifier;
@end

In order for the XYZPerson class to be able to change the property internally, it makes sense to redeclare the property in a class extension that’s defined at the top of the implementation file for the class:

@property (readwrite) NSString *uniqueIdentifier;

Note: The readwrite attribute is optional, because it’s the default. You may like to use it when redeclaring a property, for clarity.

毁梦 2024-12-07 15:14:09

类别是 Objective-C 语言的一项功能,可让您向现有类添加新方法。扩展是类别的一种特殊情况,它允许您定义必须在主实现块中实现的方法。

私有声明可以在类扩展中,主要是一些属性,因为我们不需要在调用方法之前声明它。

Categories are an Objective-C language feature that let you add new methods to an existing class. Extensions are a special case of categories that let you define methods that must be implemented in the main implementation block.

Private declarations can be in class extensions, which mainly are some properties, because we have no need to declare a method before we call it.

泛泛之交 2024-12-07 15:14:09

ios扩展类似于c#、java抽象类或接口

ios类别类似于c#类扩展

ios extension similiar to c#,java abstract class or interface

ios category similiar to c# class extension

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文