iDeviceGuy
iDeviceGuy

Reputation: 993

New to iOS. Expected expression error?

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

#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

Upvotes: 3

Views: 11690

Answers (2)

John Pitts
John Pitts

Reputation: 971

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)];

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

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"); }];

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:

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

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

Upvotes: -1

Stephen
Stephen

Reputation: 1436

In the method

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

you are missing a closing bracket before

return self;

Upvotes: 8

Related Questions