Pergunta

Possible Duplicate:
How can I solve this Problem with bidirectional dependencies in Objective-C classes?

Why am I getting a "Parse Issue - Expected a Type" with the following code and what can I do to get it resolved (without changing the API of my test functions)?

// ClassA.h

#define CLASSA_H

#import <Foundation/Foundation.h>
#ifndef CLASSB_H
#import "ClassB.h"
#endif

@interface ClassA : NSObject {}

- (void)test:(ClassB *)input;  // Compiler error: Expected ClassB to be a type

@end

and

// ClassB.h

#define CLASSB_H

#import <Foundation/Foundation.h>
#ifndef CLASSA_H
#import "ClassA.h"
#endif

@interface ClassB : NSObject{}

- (void)test:(ClassA *)input;  // Compiler error: Expected ClassA to be a type

@end

Based on what I read about import vs. include, I shouldn't even have to use the CLASSA_H and CLASSB_H macros to prevent self-inclusion, but no matter whether I use them or not, I get the very same error.

My actual code is more complex than the sample above, but it boils down to my having two classes whose interfaces reference each other, thus requiring each header to include the other one as in the example above. This is a common situation in C and I don't get what I am missing here.

Foi útil?

Solução

You don't need to include either header. Use a forward declaration.

// ClassA.h

#import <Foundation/Foundation.h>

@class ClassB;

@interface ClassA : NSObject {}

- (void)test:(ClassB *)input;

@end

and

// ClassB.h

#import <Foundation/Foundation.h>

@class ClassA;

@interface ClassB : NSObject {}

- (void)test:(ClassA *)input;

@end

Depending on your situation, you might also need to #import "ClassA.h" inside of ClassB.m, and vice-versa. But that won't cause any additional problems.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top