iphone - 为什么在 objective-c 中使用 @class

标签 iphone ios compiler-directives

<分区>

Possible Duplicate:
@class May I know the proper use of this

我想知道为什么要使用@class。我有一个普遍的理解,它允许你访问你调用的那个类中的东西,但是我不知道它的好处..

最佳答案

@class 指令设置了对另一个类的前向引用。它告诉编译器指定的类存在,因此当编译器到达 @property 指令行时,不需要其他信息,它假设一切都很好并继续前进。

例如,这段代码可以单独运行:

#import <UIKit/UIKit.h>
#import "MyExampleClass"

@interface CFExampleClass : NSObject <SomeDelegate> {
}

@property (nonatomic, strong) MyExampleClass *example;

@end

但是,假设我们想避免循环包含这些 header (例如 CFExampleClass 导入 MyExampleClassMyExampleClass 导入 CFExampleClass), 然后我们可以使用 @class 告诉编译器 MyExampleClass 存在而不会有任何提示。

#import <UIKit/UIKit.h>
@class MyExampleClass;

@interface CFExampleClass : NSObject <SomeDelegate> {
}

@property (nonatomic, strong) MyExampleClass *example;

@end

关于iphone - 为什么在 objective-c 中使用 @class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10809562/

相关文章:

ios - 在 ios 8.0 中集成 map

android - Firebase:报告分析和崩溃数据以分离项目

c# - 避免在c#中对一段代码进行调试和调用堆栈

ios - 在 Objective-C iOS 中替换去电屏幕

ios - Xcode 10.x 中 iOS 纵向和横向的替代布局

string - Delphi 短字符串编译器指令不起作用?

c# - 摆脱 C# 中的预编译器指令

以 root 身份运行的 iOS 应用程序拒绝关闭

iphone - 如何将设置分组

iphone - iPhone模拟器查看sqlite3数据库