Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我是否可以比较一个字符串(一个单词)和一个由用户输入的字母,然后作为BOOL接收输出_Ios_String_Compare_String Search - Fatal编程技术网

Ios 我是否可以比较一个字符串(一个单词)和一个由用户输入的字母,然后作为BOOL接收输出

Ios 我是否可以比较一个字符串(一个单词)和一个由用户输入的字母,然后作为BOOL接收输出,ios,string,compare,string-search,Ios,String,Compare,String Search,我是IOS开发者的新手,正在制作简单的程序。这是一款刽子手游戏。 我想从plist文件中选择一个随机字符串(已完成)。 现在,我想比较用户输入的文本(来自文本字段),并将其与我们从plist中随机选取的字符串进行比较 这是我的MainViewController.m代码,因为它是一个实用程序。当前仅使用MainView #import "MainViewController.h" #import "WordListLoad.h" @interface MainViewController ()

我是IOS开发者的新手,正在制作简单的程序。这是一款刽子手游戏。 我想从plist文件中选择一个随机字符串(已完成)。 现在,我想比较用户输入的文本(来自文本字段),并将其与我们从plist中随机选取的字符串进行比较

这是我的MainViewController.m代码,因为它是一个实用程序。当前仅使用MainView

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


}
这就是我要执行检查的地方 我尝试过characterAtIndex,但我似乎无法让它对放置在字符串中的硬编码起作用,更不用说使用for语句来系统地检查字符串了

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

}

要比较两个字符串:
[string1等于字符串:string2]
。如果string1等于string2,则返回true。要获取UITextfield中包含的字符串:
textfield.text

对于您的检查方法,您发送的是UITextfield本身,而不是它的文本字符串。相反,请尝试:

[self check: self.textfield.text];
您还需要创建一个NSString属性来保存plist中的随机字符串,以便以后可以访问并与textfield字符串进行比较,如下所示:

在类的接口中声明:

@property (nonatomic,strong) NSString* randomString;
在启动方法中:

self.randomString = [swords objectAtIndex:randomIndex];
在检查方法中:

return [self.randomString isEqualToString:randomString];

鉴于这是一个刽子手游戏,我假设您正在尝试查看一个给定字符串是否包含一个字母-因此equalsToString:不是您想要的

相反,使用可能更好

此外,正如Patrick Goley所指出的,您需要确保使用textfield.text值从中获取字符串。与存储要用作隐藏单词的初始单词相同

还有一些小的代码问题(例如,函数头中的分号),您需要清理这些问题才能使应用程序正常运行


编辑:使字符串调用的范围实际使用textfield的文本,并且不区分大小写(以防止用户在单词小写时输入大写字母时返回错误,反之亦然)。还包括指向

欢迎使用堆栈溢出的文档的链接!让您的问题得到更快回答的一种方法是提供您拥有的代码,并在您不了解如何编写的部分编写简短的注释。祝你好运@dasblinkenlight谢谢我会的。考虑到这是一个刽子手游戏,我想他是想看看一个字母是否包含在一个给定的字符串中——所以equalsToString:不是他想要的。相反,考虑到这是一个刽子手游戏,可能更好地使用它,我假设他正在尝试查看一个字母是否包含在一个给定的字符串中——所以equalsToString:不是他想要的。相反,最好使用[string1 rangeOfString:string2]。位置!=nsnotfound这帮了大忙,谢谢@Doc。我唯一的问题是我不知道该如何分辨这封信在哪个地方。例如单词log,有没有办法确定字母o位于单词的第二个位置?非常感谢。假设rangeOfString:options:的返回值不返回NSNotFound,那么它将返回一个NSRange结构:请记住,它只返回搜索词的第一个匹配项。您需要再次运行rangeOfString:options:range:以查看它是否存在于多个位置(其中range排除第一个结果之前的所有内容)。
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
}