Is there anyway I can compare a String (which is a word) and a letter which is input by the user and receive an output as a BOOL

StackOverflow https://stackoverflow.com/questions/17407461

Question

I'm new to IOS dev and am making simple programs this one is a hangman game. I wanted to pick a random string from a plist file (completed). I now want to compare the user input text (from a text field) and compare it to the string we have randomly picked from our plist.

Here is my code for MainViewController.m as it is a utility. Only the MainView is being used currently.

#import "MainViewController.h"
#import "WordListLoad.h"
@interface MainViewController ()
@end
@implementation MainViewController
@synthesize textField=_textField;
@synthesize button=_button;
@synthesize correct=_correct;
@synthesize UsedLetters=_UsedLetters;
@synthesize newgame=_newgame;
- (IBAction)newg:(id)sender
{
[self start];
}
- (void)start
{
NSMutableArray *swords = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swords" ofType:@"plist"]];
NSLog(@"%@", swords);
NSInteger randomIndex = arc4random() % [swords count];
NSString *randomString = [swords objectAtIndex:randomIndex];
NSLog(@"%@", randomString);


}

This is where i would like to implement the checking I have tried characterAtIndex and I can't seem to get it to work for hard coded placed in the string let along using a for statement to systematic check the string.

- (void)check: (NSString *) randomString;
{
//NSLogs to check if the values are being sent

NSLog(@"2 %@", self.textField.text);

}
- (IBAction)go:(id)sender
{
[self.textField resignFirstResponder];

NSLog(@"1 %@", self.textField.text);
[self check:(NSString *) self.textField];
_textField.text = nil;


}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self start];

}
Was it helpful?

Solution 2

Given that it's a hangman game, I assume you are trying to see if a single letter is contained by a given string - so equalsToString: wouldn't be what you want.

Instead, probably better to use rangeOfString:options:

if ([randomString rangeOfString:self.textfield.text options:NSCaseInsensitiveSearch].location != NSNotFound){
    // Do stuff for when the letter was found
}
else {
    // Do stuff for when the letter wasn't found
}

Also, as was pointed out by Patrick Goley, you need to make sure you're using the textfield.text value to get the string from it. Same with storing the initial word you'll be using as the hidden word.

There are also a couple of other minor code issues (semicolon in the function header, for example) that you'll need to clean up to have a functioning app.

Edit: Made the range of string call actually use the textfield's text, and do so case-insensitive (to prevent false returns when a user puts a capital letter when the word is lower case, or vice-versa). Also included link to documentation of NSString's rangeOfString:options:

OTHER TIPS

To compare 2 strings: [string1 equalsToString:string2]. This will return true if string1 is equal to string2. To get the string contained in a UITextfield: textfield.text.

For your check method you are sending the UITextfield itself, instead of its text string. Instead try:

[self check: self.textfield.text];

You'll also need to create an NSString property to save your random string from the plist, so you can later access to compare to the textfield string like so:

declare in the interface of the class:

@property (nonatomic,strong) NSString* randomString;

in the start method:

self.randomString = [swords objectAtIndex:randomIndex];

in the check method:

return [self.randomString isEqualToString:randomString];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top