jeudi 8 juin 2017

Why won't an if statement work when I am using an NSString and a UIlabel to see if they match?Why do I need == for an if statement and not =?

I am creating a app like hang man where the user will enter a letter in a text box and press a button to see if the letter is correct and if it is the letter will replace a dash in the correct location. I am not doing graphics for this game. If the letter is wrong, it will then just increment a variable that I created. I am having trouble with an if statement though... I got the code to work and the user is able to guess the correct word but my if statement for if the correct word is equal to the guessed word won't work. What I want this if statement to do is creat a pop up that says "Winner! You won!". This is the code I have for the button where this if statement should take place.

@property (weak, nonatomic) IBOutlet UILabel *guesslet; //the guessed letter 
@property (weak, nonatomic) IBOutlet UILabel *Word;  //the ongoing guessed word
@property (weak, nonatomic) NSString *wrand;  //the correct word
@property (weak, nonatomic) NSString *letterguess;
- (IBAction)GuessButton;   //the button to guess the word

- (IBAction)GuessButton
{
  letterguess = guesslet.text;
  bool match = NO;
  char charInput = [guesslet.text characterAtIndex: 0];
  for(NSInteger i = 0; i < wrand.length; i++)
  {
    char tempChar = [wrand characterAtIndex: i];
    if (charInput == tempChar)
    {
      match = YES;
      NSRange InputRange = NSMakeRange(i, 1); //location, length
     Word.text = [Word.text stringByReplacingCharactersInRange: 
     InputRange withString: letterguess];
    }
    if(Word.text == randword)
    {
      UIAlertController *alert = [UIAlertController 
      alertControllerWithTitle:@”Winner!!”
      message:@”Great Job! You won!”
      preferredStyle:UIAlertControllerStyleAlert];
      UIAlertAction *okAction = [UIAlertAction
      actionWithTitle:@”Ok”
      style: UIAlertActionStyleDefault
      handler:^(UIAlertAction * action){
      }];
      UIAlertAction *cancelAction = [UIAlertAction
      actionWithTitle:@”Cancel”;
      style: UIAlertActionStyleDefault
      handler:^(UIAlertAction * action){
      }];
      [alert addAction:okAction];
      [alert addAction:cancelAction];
      [self presentViewController:alert animated: YES completion:nil];


    }
}

if(match == NO)

The code keeps going but is not necessary. My alert message is the one not working. Is there a way to get it to work and why isn't it working?

Aucun commentaire:

Enregistrer un commentaire