@class在Objective-C中做什么? [英] What does @class do in Objective-C?

查看:108
本文介绍了@class在Objective-C中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我解释@class声明在Objective-C中的作用以及在什么情况下应使用此声明?

Can anyone please explain me what does @class declaration do in Objective-C and what are the cases in which we should use this declaration?

推荐答案

这是一个前向声明.从本质上讲,它告诉编译器有一个该名称的类.我在接口声明中使用它:

It’s a forward declaration. It essentially tells the compiler that there’s a class of that name. I use it in the interface declarations:

@class Foo;
@interface Bar : NSObject {
    Foo *someFoo;
}
@end

当然,您可以改为导入Foo的标头:

Of course you could import the header for Foo instead:

#import "Foo.h"
@interface Bar : NSObject {
    Foo *someFoo;
}
@end

但是,如果someFoo不会向Bar的用户公开,则他们将导入一个多余的头文件,这对他们没有用.使用@class声明,Bar的用户不会看到额外的导入,因为Foo.h将被导入到Bar的实现文件中.

But if someFoo is not exposed to users of Bar, they would import an extra header file that’s of no use to them. With the @class declaration the users of Bar see no extra import, because Foo.h will be imported in the implementation file of Bar.

这篇关于@class在Objective-C中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆