如何在 Objective-C 中将对象添加到数组中?

发布于 2024-12-07 07:37:06 字数 2584 浏览 1 评论 0原文

我开始用 Objective-C 编程。我正在使用一本书来学习第一基础知识。给定的示例不起作用!我在代码中看不到问题。
理解它并不难,但就是行不通。但我在其他地方找到的所有东西,他们都像我一样添加了对象。
不知道语法有没有改变?我正在使用 iOS 4.3 SDK 和 Xcode 3.2.6!?
这部分失败:

[questions addObject: @”What is 7 + 7?”]; //FAILS
[answers addObject: @”14”];

错误消息显示:
/Applications/Quiz/Classes/QuizAppDelegate.m:32:0
应用程序/测验/类/QuizAppDelegate.m:32:错误:“@”标记之前的预期表达式

如果有人可以帮助我,我会非常高兴! 谢谢你!

我也附上了代码!

Jules

完整代码:.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];
} 

I am starting to program in Objective-C. I am using a book to learn the first basics. The given example is not working! I can not see the problem in my code.
It was not so hard to understand it, but it just won't work. But everything I found elsewhere, they added Objects like I did.
I don't know if the syntax changed? I am using the iOS 4.3 SDK and Xcode 3.2.6!?
This part fails:

[questions addObject: @”What is 7 + 7?”]; //FAILS
[answers addObject: @”14”];

The error message says:
/Applications/Quiz/Classes/QuizAppDelegate.m:32:0
Applications/Quiz/Classes/QuizAppDelegate.m:32: error: expected expression before '@' token

I would be really happy, if someone can help me!
Thank you!

I attached the code as well!

Jules

Full code: .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

Part code where it fails: .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];
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-12-14 07:37:07

我认为,您在双引号“中使用了假字符,就像您的一样”。您是否从某处复制并粘贴了代码?

I think, you are using the false character for the double quote ", as yours is ”. Did you copy and paste the code from somewhere?

月寒剑心 2024-12-14 07:37:07

或者甚至更短:

[myArray addObject:[NSString stringWithString:@"Wie hoch ist der Eiffelturm?"]];

Or even shorter:

[myArray addObject:[NSString stringWithString:@"Wie hoch ist der Eiffelturm?"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文