0
#import <Foundation/Foundation.h>
#import "Asset.h"

@interface Person : NSObject{
    int pin;
    NSMutableArray* assets;
}

@property int pin;
-(void) addAsset: (Asset*) iasset; //producing error
@end

Trying to code the interface of a "Person" class that contains an array of "assets". The line

 -(void) addAsset: (Asset*) iasset;

produces an error. XCode says, "Expected a type". Can someone tell me where I'm going wrong? I can provide whatever other code is needed.

Asset.h :

#import <Foundation/Foundation.h>
#import "Person.h"

@interface Asset : NSObject{
    NSString* label;
    int value;
    Person* holder; 
}
@property int value;
-(void) setHolder: (Person*)iholder;
-(void) setLabel: (NSString*)iname;
@end
Byron S
  • 99
  • 2
  • 9

1 Answers1

4

You have a circular dependency in your header files. You can fix it by removing #import "Person.h" in Asset.h and replacing it with @class Person;. This change will tell the compiler about the existence of the Person class without requiring the header to be imported.

Likewise, you could instead replace #import "Asset.h" in Person.h with @class Asset;.

You'll still want to include the correct headers from your implementation files.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Thank you sir! Could you explain to me how I go about choosing between @class and #import? My book brought it up but did very little explaining. – Byron S May 28 '13 at 23:52
  • Use `#import` when possible I guess? It makes your life (marginally) easier. If you have to break a cycle, pick a spot and use `@class`. That's it. – Carl Norum May 28 '13 at 23:53
  • @Kevin's advice seems reasonable to me. Let's go with that. – Carl Norum May 28 '13 at 23:54
  • @ByronShilly - In general, if you're only referencing the class as a pointer, you can use `@class`. (You do then need to remember to `#import` the Asset.h file in the Person.m file, though.) I tend to use `@class` much of the time, just on the theory that it compiles a little faster. – Hot Licks May 28 '13 at 23:54