开发者

Compiler error - iOS "expected ")" before "Question

开发者 https://www.devze.com 2023-04-01 17:44 出处:网络
What\'s wrong with the following code? Xcode 4 is saying that the two method declarations with \"Question\" don\'t compile due to an \'\"expected \")\" before \"Question\"; message, as indicated in th

What's wrong with the following code? Xcode 4 is saying that the two method declarations with "Question" don't compile due to an '"expected ")" before "Question"; message, as indicated in the comments in the code below. The Question class compiles and this code has been working previously. I made some changes to Questions, but backed them out to try to figure out this compile time error.

#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)       开发者_StackOverflow resetState;
- (Question *)  currentQuestion; //  <=====   error


@end

Here's Question.h:

#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


Circular dependency? AppState is importing Question and Question is importing AppState.

Forward-declare one of them to break the cycle, e.g. use @class Question before your AppState @interface statement, like this

@class Question;

@interface AppState : NSObject {

    int chosenAnswer;
    int correctAnswers;
    int currentQuestionNumber;

   // this will contain the hash table of question objects
   NSMutableDictionary *questionHash;
}
...

Related question: @class vs. #import


When you #import "AppState.h" in Question you make a cyclic dependency. It would be best to move #import "AppState.h" and #import "Question.h" to implementation part. In the header just leave

@class Question;

and

@class AppState;

before interface declaration.

0

精彩评论

暂无评论...
验证码 换一张
取 消