chartman
chartman

Reputation: 172

using @property for a class I implemented

I created two classes in objective c and I would like to use one of them as a property of the other. To be more concrete, one of the classes is a 'term' that contains an integer variable and an nsstring that acts as the variable. The other class is an 'expression' that has an nsmutable array as an instance variable that contains 'terms' as its objects. What I want to do is have add the possibility of having one of the terms have an 'expression' as a property to implement something like distributing over parentheses and substituting an expression for a variable. However, Xcode is telling me that 'expression' is not an acceptable type name despite the fact that I have imported my expression header file. I think I may have read somewhere that only foundation classes are available to use as properties. What can I do to add this class as an instance variable?

Upvotes: 0

Views: 4018

Answers (2)

rishabh
rishabh

Reputation: 1165

Although the above answers are also correct, in my case the problem which occured was "@end" was missing in prototype/interface declaration.

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385500

I suspect you have an import cycle, like this:

Expression.h

#import "Term.h"

@interface Expression : NSObject
...

Term.h

#import "Expression.h"

@interface Term : NSObject
...

Notice how each file imports the other? That won't work. Instead, you need to use forward declarations:

Expression.h

@class Term;  // tell the compiler that Term is a class name

@interface Expression : NSObject
...

Term.h

@class Expression;  // tell the compiler that Expression is a class name

@interface Term : NSObject
...

Then, in your .m files, you can safely import both .h files:

Expression.m

#import "Expression.h"
#import "Term.h"

@implementation Expression
...

Term.m

#import "Term.h"
#import "Expression.h"

@implementation Term
...

Upvotes: 2

Related Questions