以下代码有什么问题? Xcode 4说两个方法声明用"问题"由于'"期待")"而不能编译。之前"问题&#34 ;;消息,如下面代码中的注释所示。问题类编译,此代码以前一直在工作。我对问题进行了一些更改,但是将它们退出来试图弄清楚这个编译时错误。
#import <Foundation/Foundation.h>
#import "Question.h"
@interface AppState : NSObject {
int chosenAnswer;
int correctAnswers;
int currentQuestionNumber;
// this will contain the hash table of question objects
NSMutableDictionary *questionHash;
}
@property (nonatomic) int chosenAnswer;
@property (nonatomic) int correctAnswers;
@property (nonatomic) int currentQuestionNumber;
@property (nonatomic, retain) NSDictionary *questionHash;
- (void) printQuestions;
- (void) printDescription;
- (void) addQuestion: (Question *) question; // <==== error
- (int) numberOfQuestions;
- (void) saveState;
- (void) resetState;
- (Question *) currentQuestion; // <===== error
@end
这里的问题:
#import <Foundation/Foundation.h>
#import "AppState.h"
@interface Question : NSObject {
NSString *questionTxt;
int correctAnswer;
int number;
// this will contain the hash table of questions_answer objects
NSMutableDictionary *answerHash;
}
@property (nonatomic, retain) NSString * questionTxt;
@property (nonatomic) int correctAnswer;
@property (nonatomic) int number;
@property (nonatomic, retain) NSMutableDictionary *answerHash;
-(void) addAnswer: (NSString *) answer;
- (NSMutableArray *) answerArray;
- (void) printDescription;
- (void) printAnswers;
- (NSString *) correctAnswerText;
- (Question *) currentQuestion;
@end
答案 0 :(得分:4)
循环依赖? AppState正在导入问题,而问题正在导入AppState。
转发声明其中一个以打破周期,例如在AppState @interface语句之前使用@class Question,就像这样
@class Question;
@interface AppState : NSObject {
int chosenAnswer;
int correctAnswers;
int currentQuestionNumber;
// this will contain the hash table of question objects
NSMutableDictionary *questionHash;
}
...
相关问题:@class vs. #import
答案 1 :(得分:0)
当您在问题中#import“AppState.h”时,您会产生循环依赖。 最好将#import“AppState.h”和#import“Question.h”移动到实现部分。在标题中留下
@class Question;
和
@class AppState;
在接口声明之前。