what does @class do in iOS 4 development?(@class 在 iOS 4 开发中做了什么?)
问题描述
做起来有什么区别
@class MyViewController;
而不是将 .h 正常导入到 appdelegate.h 中
rather than doing the normal import of the .h into the appdelegate.h
#import "MyViewController.h"
我最近看到了一些使用@class 方式的示例,想知道是否有任何区别.
I've seen some example recently that use the @class way and wondered if there any differences.
谢谢.
推荐答案
有很大的不同.
@class MyViewController;
是对象 MyViewController 的前向声明.当你只需要告诉编译器一个对象类型而不需要包含头文件时使用它.
Is a forward declaration for the object MyViewController. It is used when you just need to tell the compiler about an object type but have no need to include the header file.
但是,如果您需要创建这种类型的对象并在其上调用方法,则需要:
If however you need to create an object of this type and invoke methods on it, you will need to:
#import "MyViewController.h"
但通常这是在 .m 文件中完成的.
But normally this is done in the .m file.
前向声明的另一个用途是在与使用它的对象相同的头文件中定义 @protocol.
An additional use of forward declarations is when you define a @protocol in the same header file as an object that uses it.
@protocol MyProtocolDelegate; //forward declaration
@interface MyObject {
id<MyProtocolDelegate> delegate;
...
}
...
@end
@protocol MyProtocolDelegate
... //protocol definition
@end
在上面的例子中,编译器需要知道 @protocol MyProtocolDelegate 是有效的,然后才能编译 MyObject 对象.
In the above example the compiler needs to know that the @protocol MyProtocolDelegate is valid before it can compile the MyObject object.
只需将协议定义移到 MyObject 定义之上也可以.
Simply moving the protocol definition above MyObject definition would also work.
这篇关于@class 在 iOS 4 开发中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:@class 在 iOS 4 开发中做了什么?
基础教程推荐
- Android文本颜色不会改变颜色 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
