xcode iOS 新手。预期的表达错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14168253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:27:57  来源:igfitidea点击:

New to iOS. Expected expression error?

objective-ciosxcode

提问by STANGMMX

It seems unusual as the method is the exact same as my showAnswer method, so I thought I'd ask here.

这似乎不寻常,因为该方法与我的 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

回答by Stephen

In the method

在方法中

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

you are missing a closing bracket before

你之前缺少一个右括号

return self;

回答by John Pitts

Encountered this frustrating error "Expected Expression" in an Objective-C function call (to a fetcher for musical artist from Audio DB API) which looked like this: [_artistController fetchArtistWith:searchText completionBlock:^(NSArray * _Nonnull bands, NSError * _Nonnull error)];

在 Objective-C 函数调用中遇到这个令人沮丧的错误“预期表达式”(从 Audio DB API 到音乐艺术家的提取器),如下所示: [_artistController fetchArtistWith:searchText completionBlock:^(NSArray * _Nonnull bands, NSError * _Nonnull error)];

Finally realized they were asking for an 'expression' which in iOS languages typically means code inside squiggly brackets '{....}'

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

So changed function call temporarily (to get rid of error and run program) to this...

因此暂时将函数调用(以消除错误并运行程序)更改为...

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

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

FYI: what's inside brackets should be error handling mostly

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

Interestingly, XCode doesn't care if you pre-define variables error or bands-- you can make them anything you like, but both should likely be USED in the expression brackets-- hence error-handling with 'error'. Those are considered type-inferred--> the same as is done by Swift and Objective-C for a variable introduced in a For-in loop such as "i" in the common looping method:

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

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

So DON'T FORGET YOUR BRACKETS {....} TO HANDLE YOUR CLOSURE!!

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