我開始在Objective-C中編程。我正在使用一本書來學習第一本基礎知識。給出的例子不起作用!我在代碼中看不到問題。
這不是很難理解它,但它不會工作。但是我在別處找到的所有東西,他們像我一樣添加了對象。
我不知道語法是否改變了?我正在使用iOS 4.3 SDK和Xcode 3.2.6!
這部分失敗:如何將對象添加到Objective-C中的數組?
[questions addObject: @」What is 7 + 7?」]; //FAILS
[answers addObject: @」14」];
該錯誤消息表示:
/Applications/Quiz/Classes/QuizAppDelegate.m:32:0
應用/測驗/類別/ QuizAppDelegate.m:32:錯誤: '@'令牌之前的預期表達式
如果有人能幫助我,我會非常高興! 謝謝!
我附上了代碼!
朱
全碼:.H:
#import <UIKit/UIKit.h>
@interface QuizAppDelegate : NSObject <UIApplicationDelegate> {
int currentQuestionIndex;
//The model objects
NSMutableArray *questions;
NSMutableArray *answers;
//The view objects
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
-(IBAction)showQuestion:(id)sender;
-(IBAction)showAnswer:(id)sender;
@end
部分代碼而失敗:.M:
#import "QuizAppDelegate.h"
@implementation QuizAppDelegate
@synthesize window;
-(id)init{
// Call the init method implemented by the superclass
[super init];
// Create two arrays and make the pointers point to them
questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];
// Add questions and answers to the arrays
[questions addObject: @」What is 7 + 7?」]; //FAILS
[answers addObject: @」14」];
[questions addObject: @」Was ist die Hauptstadt von Madagaskar?」]; //FAILS
[answers addObject: @」Antananarivo」];
[questions addObject: @」Was ergibt 5-2*2+6?」]; //FAILS
[answers addObject: @」7」];
// Return the address of the new object
return self;
}
-(IBAction)showQuestion:(id)sender{
//Step to the next question
currentQuestionIndex++;
//Am I past the last question?
if(currentQuestionIndex==[questions count]){
//Go back to the first question
currentQuestionIndex=0;
}
// Get the string at that index in the questions array
NSString *question = [questions objectAtIndex:currentQuestionIndex];
//Log the string to the console
NSLog(@"displaying question:%@",question);
//Display the string in the question field
[questionField setText:question];
//Clear the answer field
[answerField setText:@"???"];
}
-(IBAction)showAnswer:(id)sender{
//What is the answer to the current question
NSString *answer=[answers objectAtIndex:currentQuestionIndex];
//Display it in the answer field
[answerField setText:answer];
}
我認爲,你使用的是「,你的是」的假字符,你是從哪裏複製和粘貼代碼的嗎? – vikingosegundo
他是對的,看看你的「NSLog」語句中的雙引號。 (正確)字符 – Brad
@vikingosegundo你應該加上那個答案! – mydogisbox