-3

i can't get the use of #import and @class

let's try to make things clear with a simple classical example: the employee case.

I have a class employee, a class manager and a class department.

Employee is superclass of manager and also contain an object department ( so where should i import department? in employee.h or employee.m and should i use #import or @class ? )

Department contains an array of employees so it should so should import the class employee.h ( should i use import or class? and where? )

i know theres a better way to solve this problem, using databases or so
but that's not my point, i want to understand when and where to use
#import and where and when use @class

i tried to read similar post but i didn't understand how that works...

mystudioos
  • 99
  • 1
  • 1
  • 8

1 Answers1

3

Using @class is just a hint to the compiler that you mean to use that class and it can be used when you are only referencing this class in some kind of declaration. It will not import the header file and therefore will compile a little faster. It is also useful to avoid circular inclusions.

Using #import will actually import the header file during compilation and is needed when you are relying on the behavior of the class in question. That is how the compiler knows what properties, methods, etc can be used safely.

Ross Hambrick
  • 5,880
  • 2
  • 43
  • 34
  • ok here's my doubts... if i want to import a class it's actually cause i need to work with an object of that class, using one of it's method or getting some variable values... can you please make a practical example of when you use @class ? other doubt is about when using those keywords. some people say to import the class in the header file some other say to import in the .m file... what's the difference? – mystudioos Jul 31 '13 at 20:23
  • 1
    I use @class all the time in header files where I'm just declaring an ivar or property (or whatever) with a type of that class. In that case, there is no need to import the header. In that implementation file however, I would need to import the header to use an instance of that class. – Ross Hambrick Jul 31 '13 at 20:25