ios - #import 和@class 之间的区别

标签 ios xcode class import

<分区>

我最近看了一个教程,演讲者说如果你使用它没有什么区别:

#import "Class.h"

或:

@class Class;

而且我不得不说,我的应用程序以一种或另一种方式工作。 但一定有区别吧?所以我想问问你们。

提前致谢!

最佳答案

根据 Apple 文档:

@class allows you to declare that a symbol is an Objective-c class name without the need to #import the header file that defines the class.

You would use this where you only need the class name defined for the purposes of declaring a pointer to the class or a method parameter of the class, and you do not need to access any methods, fields, or properties in the class.

It saves a minuscule amount of compile time vs the #import, and it sometimes helps avoid messy include circularity issues.

来自 here 的好答案

例如当你创建一个协议(protocol)时:

@class yourCustomView;

@protocol yourCustomViewDelegate <NSObject>

@required

- (void) somethingDidInView:(UIView*)view;

@end

@interface yourCustomView : FIView

关于ios - #import 和@class 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19465059/

相关文章:

c# - 多线程锁和监视器类不起作用

python - 构造函数未能接受输入参数

ios - 一个 iOS 应用程序可以有多个窗口吗?

ios - 在Xcode中使用代码完成时如何选择第二个参数?

c# - 如何在另一个类中传递不同的类对象并期望它们得到类似的对待?

ios - 如何更新添加的 pod 文件?语法错误

iphone - 错误 : 0xe8003ffe when sync . 来自 XCODE 4.2 的 ipa 文件临时分发

iphone - iPhone 上的二进制短信

ios - SceneKit 旋转节点两次只执行最后一次旋转?

iphone - 设备的 "Settings"应用程序中缺少应用程序设置属性 - iOS