3

这似乎不寻常,因为该方法与我的 showAnswer 方法完全相同,所以我想我会在这里问。

#import "QuizViewController.h"

@interface QuizViewController ()

@end

@implementation QuizViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
// Call the init method implemented by the superclass
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // 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?"];
    [answers addObject:@"14"];

    [questions addObject:@"What is the capital of Vermond?"];
    [answers addObject:@"Montpelier"];

    [questions addObject:@"From what is cognac made?"];
    [answers addObject:@"Grapes"];

    //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];
}


}
@end
4

2 回答 2

8

在方法中

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

你之前缺少一个右括号

return self;
于 2013-01-05T03:37:47.380 回答
-1

在一个 Objective-C 函数调用中遇到了这个令人沮丧的错误“Expected Expression”(来自 Audio DB API 的音乐艺术家的提取器),它看起来像这样: [_artistController fetchArtistWith:searchText completionBlock:^(NSArray * _Nonnull bands, NSError * _Nonnull error)];

终于意识到他们要求的是一个“表达式”,在 iOS 语言中这通常意味着在波浪括号“{....}”中的代码

所以暂时改变了函数调用(以消除错误并运行程序)到这个......

[_artistController fetchArtistWith:searchText completionBlock:^(NSArray * _Nonnull bands, NSError * _Nonnull error) { NSLog(@"do something with bands or error here"); }];

仅供参考:括号内的内容应该主要是错误处理

有趣的是,XCode 并不关心你是否预定义了变量 error 或 band ——你可以将它们设为任何你喜欢的东西,但两者都应该在表达式括号中使用——因此使用“error”进行错误处理。这些被认为是类型推断的--> 与 Swift 和 Objective-C 对 For-in 循环中引入的变量(例如常见循环方法中的“i”)所做的相同:

 for i in seriesOfNumbers {...
The i is also type-inferred.

所以不要忘记你的括号{....}来处理你的关闭!

于 2019-07-20T13:11:25.297 回答