开发者

Why do header files in Objective-C have both import and class statements at the top?

What is the diffe开发者_JAVA技巧rence between the two?


A forward class declaration (class statement) simply tells the compiler a certain class exists, without specifying its methods, properties, etc. You can use a forward class declaration when you only need to declare a variable of that class, which is what you need in your header files most of the time. Then you will have to import the class in the implementation file.

Forward class declarations are particularly useful in resolving circular dependencies.

Check this out for more info:

  • @class vs. #import
  • Objective-C: Forward Class Declaration


The @class declaration is a way of telling the compiler that a particular class type exists without providing it with a full declaration of the class interface. This is useful when you want to refer to a class without disclosing/referencing methods which it exposes. In other words using a class declaration you can refer to a class in a header file and then delay the import of the implementation to your .m file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜