跨类声明错误?

时间:2012-04-29 14:55:53

标签: iphone objective-c ios

我正在尝试使用Restkit进行映射并创建了2个类,如下所示。我收到了以下错误:

  • Campaign.h中的未知类型名称“卡片”
  • Card.h
  • 中的未知类型名称“广告系列”
  • 具有“保留(或强)”属性的属性必须为对象类型
  • ...更多但类似的错误

我的问题是有一种方法可以通过重用类来实现下面的类声明。

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

2 个答案:

答案 0 :(得分:2)

通常,在标头中,您使用前向类声明,以避免导入冲突。因此,在Campaign.h中,在您的界面之前,您已经拥有@class Card,而在Card.h中,您拥有@class Campaign。这只是告诉编译器这些类是否存在&在其他地方定义;这通常只需要在标题中知道。

答案 1 :(得分:1)

以防有人将来需要它。在这里我的解决方案:

<强> 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