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

时间:2013-01-05 03:23:02

标签: objective-c ios xcode

这个方法与我的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

2 个答案:

答案 0 :(得分:8)

方法

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

你错过了

之前的结束括号
return self;

答案 1 :(得分:-1)

在Objective-C函数调用(从Audio DB API到音乐艺术家的提取程序)中遇到了以下令人沮丧的错误“ Expected Expression”: [_artistController fetchArtistWith:searchText completionBlock:^(NSArray * _Nonnull bands, NSError * _Nonnull error)];

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

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

[_artistController fetchArtistWith:searchText completionBlock:^(NSArray * _Nonnull bands, NSError * _Nonnull error) { NSLog(@"what do they want with Expected Expression? Perhaps error-handling code... later"); }];

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

有趣的是,XCode不在乎是否预定义变量error或band —您可以将它们定义为自己喜欢的任何东西,但是都可以在表达式括号中使用它们—因此用'error'处理错误。那些被认为是类型推断的->与Swift和Objective-C对于在普通循环方法中在For-in循环中引入的变量(例如“ i”)所做的相同:

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

因此,不要忘了使用括号{....}处理关闭!!