如何将对象添加到Objective-C中的数组?
我开始在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];
}
我认为,你使用双引号的假字符“,因为你的是”。你是否从某处复制并粘贴了代码?
或者更短:
[myArray addObject:[NSString stringWithString:@"Wie hoch ist der Eiffelturm?"]];
更短的(而且仍然是功能性的)正是他这样做的方式!(当然纠正引号) – mydogisbox
谢谢,改变了quotatin标记,它的工作! – julesmummdry
我认为,你使用的是“,你的是”的假字符,你是从哪里复制和粘贴代码的吗? – vikingosegundo
他是对的,看看你的“NSLog”语句中的双引号。 (正确)字符 – Brad
@vikingosegundo你应该加上那个答案! – mydogisbox