What's the difference between #import and @class, and when should I use one over the other?

后端 未结 3 400
花落未央
花落未央 2020-12-04 14:27

I\'ve been teaching myself Objective-C over the past month or so (I\'m a Java head) and I\'ve got my brain wrapped around most of it now. One thing that\'s confusing me at t

3条回答
  •  無奈伤痛
    2020-12-04 15:26

    The other thing you want to keep in mind is that #imports slow down your compile times, since it means the the compiler needs to pull and work through a lot more header files. This is mostly masked by the use of precompiled headers, but I have occasionally been handed projects that corss imported every header instead of using @class where appropriate, and fixing them can improve compile time. It is subtle way the the system reinforces the fact that if you only use what you actually need things go faster.

    As a general rule, I always use @class declarations in in my header files, and only #import the superclass. That falls in line with Ben's suggestions, but I thought it was worth noting that even if you are not worried about circular refs it is good idea to limit #imports in header files if you can.

提交回复
热议问题