0

I´m learning IOS/Objective-C and I have this users input Quizz project; at the moment I can show all questions and answers (with buttons and labels). But as for the next step - user inputs the answer and gets Right or Wrong - something is missing; I cant seem to match the user´s input to the index. If I write something, the output is always "Wrong". The code:

#import "QuizzViewController.h"
#import "Question.h"

@interface QuizzViewController ()

@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel;

@property NSArray *questions;
@property NSUInteger index;

@property (weak, nonatomic) IBOutlet UILabel *answerValidation;
@property (weak, nonatomic) IBOutlet UITextField *inputAction;


@property (weak, nonatomic) IBOutlet UIButton *answerButton;




@end

@implementation QuizzViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _questions = @[
                   [Question question:@"Best surfer in the World?" answer:@"Kelly Slater"],
                   [Question question:@"Capital of England?" answer:@"London"],
                   [Question question:@"Capital of Argentina?" answer:@"Buenos Aires"]
                   ];

    _index = -1;
    [self nextQuestionAction];
}



- (IBAction)answerAction:(UIButton *)sender {
    Question *question = [_questions objectAtIndex:_index];

    NSString *correct=@"Correct";
    NSString *wrong=@"Wrong";
    _inputAction.text=question.answer;

    if (question.answer==_inputAction.text){
        _answerValidation.text=correct;
    }else{
        _answerValidation.text=wrong;

    }


}



/*- (IBAction)inputAction:(UITextField *)sender {

    Question *question = [_questions objectAtIndex:_index];

    NSString *correct=@"Correct";
    NSString *wrong=@"Wrong";

    if (question.answer==_inputAction.text){
        _answerValidation.text=correct;
    }else{
    _answerValidation.text=wrong;

    }



}*/


- (IBAction)nextQuestionAction {
    _index = (_index + 1) % _questions.count;
    Question *question = [_questions objectAtIndex:_index];

    _questionLabel.text = question.question;
    _answerLabel.text = @"...";
}

- (IBAction)showAnswerAction {
    Question *question = [_questions objectAtIndex:_index];
    _answerLabel.text = question.answer;
}

@end

I guess I need to point the index, but I've tried in different ways, and it's not working...Thanks for the help

FuManchu
  • 145
  • 2
  • 11
  • `question.answer==_inputAction.text` => `[question.answer isEqualToString:_inputAction.text]`. You are comparing pointers, not the "string value". – Larme Jan 19 '17 at 16:48
  • Possible duplicate of [Understanding NSString comparison](http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison) – Larme Jan 19 '17 at 16:50
  • Is `_inputAction` `nil`? Your code seems like it would always output "Correct" if it wasn't. – dan Jan 19 '17 at 16:51
  • if (question.answer== [question.answer isEqualToString:_inputAction.text]){ _answerValidation.text=correct; }else{ _answerValidation.text=wrong; } Like this the output is the same; it compiles but I get an Warning saying im comparing a pointer to an Int – FuManchu Jan 19 '17 at 16:55
  • inputAction's code is commented – FuManchu Jan 19 '17 at 16:56
  • Well, thanks Larme, I was writing it wrong...:) It works, and thank you for the explanation – FuManchu Jan 19 '17 at 17:00

1 Answers1

0

The answer, with the help of Larme:

- (IBAction)answerAction:(UIButton *)sender {
    Question *question = [_questions objectAtIndex:_index];

    NSString *correct=@"Correct";
    NSString *wrong=@"Wrong";
    _inputAction.text=question.answer;

    if ([question.answer isEqualToString:_inputAction.text]){

        _answerValidation.text=correct;
    }else{
        _answerValidation.text=wrong;

    }


}

The comparinson has to be made with the "isEqualToString" method

FuManchu
  • 145
  • 2
  • 11