Property cannot be found in forward class object
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The Objective-C compiler error "Property 'X' cannot be found in forward class object 'Y'" occurs when you use @class to forward-declare a class but then try to access its properties without importing its header file. A forward declaration (@class ClassName;) tells the compiler a class exists but does not reveal its properties, methods, or protocol conformances. The fix is to #import the full header in the implementation file (.m) where you access properties, and use @class only in header files (.h) where you just need to reference the type.
The Error
The compiler sees Person as a forward-declared class and does not know it has a name property.
The Fix: Import the Header
By importing Person.h in the implementation file, the compiler has access to the full class interface including properties and methods.
When to Use @class vs #import
Rule of thumb: Use @class in headers, #import in implementations.
Why @class Exists
Forward declarations solve circular dependency problems. Without @class, two classes that reference each other would create an import cycle:
Protocols and Forward Declarations
The same issue occurs with protocols. Use @protocol for forward declaration:
Categories and Extensions
Accessing properties defined in a class extension or category also requires importing the relevant header:
Common Pitfalls
- Using
@classin.mfiles instead of#import: Forward declarations in implementation files prevent access to properties and methods. Always#importin.mfiles where you interact with the class. - Importing headers in other headers unnecessarily: Importing full headers in
.hfiles increases compile time and risks circular dependencies. Use@classin headers and#importin implementations. - Forgetting to import category headers: Properties and methods added in categories or class extensions are not visible without importing their specific headers, even if the base class header is imported.
- Confusing
#importwith#include:#importis an Objective-C directive that prevents duplicate inclusion automatically.#include(from C) does not prevent duplicates and requires include guards. Always use#importin Objective-C. - Not importing the header for superclass properties: If class B inherits from class A, and you forward-declare A in B's header, accessing A's properties through a B instance in the
.mfile still requires importing A's header.
Summary
- The error occurs because
@classforward declarations do not expose properties, methods, or protocols - Use
@class ClassName;in header files (.h) where you only need the type name - Use
#import "ClassName.h"in implementation files (.m) where you access properties or call methods - Forward declarations exist to prevent circular import dependencies between headers
- Always
#importthe full header of any class whose properties or methods you use in your implementation
Related reading
- Property set method not found error during reflection
- PropertyExpression is missing
- protoc object_detection/protos/.proto No such file or directory
- Protractor wait method isn't work
- Providing rabbitmq.conf in a docker-compose file gives sed cannot rename /etc/rabbitmq/sedMaHqMa Device or resource busy
- Proxy setting not working with Spring WebClient
- ps command doesn't work in docker container
- PUB/SUB pattern in ZeroMQ not working
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.