Replacing #import with @class


#1

If I replace

with

why does Xcode show a warning
no ‘+randomPossession’ method found

beside this line:

even though the app runs fine.


#2

@class only tells the file that the class Possession exists. It doesn’t tell it anything about that class, including method declarations or properties or anything else about the file Possession.h, like protocols, defines are static globals.

@class is not a replacement for importing a file, it is directive to suppress undefined symbol errors when you only need to validate the existence of a class symbol.

Methods aren’t actually called until runtime. If a class implements a method in the .m file and doesn’t declare it in the .h file, the compiler will warn you but at runtime things will work fine. In this case, the compiler doesn’t see randomPossessions (because you did not import Possessions.h) but when that method is triggered at runtime, it executes what is defined in Possessions.m.


#3

Thanks for the explanation Joe. Perhaps my confusion comes because with such a simple project, it is perhaps too early to introduce the @class concept in the book.

I also found this useful
stackoverflow.com/questions/3225 … -vs-import