Teon
Teon

Reputation: 155

Cross class declaration error?

I am trying to do mapping with Restkit and created 2 classes as below. I got the following errors:

My question is there a way to achieve below class declaration by re-using class.

Campaign.h

#import "Card.h"

@interface Campaign : NSObject

@property (nonatomic, strong) NSNumber* campaignId;
@property (nonatomic, strong) NSString* title;
@property (nonatomic, strong) Card* card;

@end

Card.h

#import "Campaign.h"

@interface Card : NSObject

@property (nonatomic, strong) NSNumber* cardId;
@property (nonatomic, strong) NSString* name;
@property (nonatomic, strong) Campaign* campaign;

@end

Upvotes: 0

Views: 297

Answers (2)

Teon
Teon

Reputation: 155

Just in case someone need it in future. Here my solution:

Campaign.h

@class Card;

@interface Campaign : NSManagedObject

@property (nonatomic, strong) NSNumber* campaignId;
@property (nonatomic, strong) NSString* title;
@property (nonatomic, strong) Card* card;

@end

Campaign.m

#import "Card.h"

@implementation Campaign

...

@end

Card.h

@class Campaign;

@interface Card : NSManagedObject

@property (nonatomic, strong) NSNumber* cardId;
@property (nonatomic, strong) NSString* name;
@property (nonatomic, strong) Campaign* campaign;

@end

Card.m

#import "Campaign.h"

@implementation Card

...

@end

Upvotes: 1

ksol
ksol

Reputation: 12235

Usually, in headers, you use forward class declarations, in order to avoid imports conflicts. So in Campaign.h, before your interface, you'd have @class Card, and in Card.h, you'd have @class Campaign. This merely tells the compiler that these class exists & are defined somewhere else; that's usually all you need to know in a header.

Upvotes: 2

Related Questions