2013-07-01 19 views
0

我是IOS新手,我正在做简单的程序这一个是一个hang子手游戏。 我想从plist文件中选取一个随机字符串(已完成)。 我现在想比较用户输入文本(来自文本字段)并将其与我们从plist中随机选取的字符串进行比较。有没有反正我可以比较一个字符串(这是一个词)和一个字母,这是由用户输入和接收输出作为BOOL

这是我的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]; 

} 
+1

欢迎堆栈溢出的文档!让你的问题得到更快回答的一种方法是提供你有的代码,并在你不懂如何编写的部分写一个简短的评论。祝你好运! – dasblinkenlight

+0

@dasblinkenlight谢谢,我会做。 – HairySocks

回答

0

鉴于这是一个刽子手游戏,我认为你是想看看一个字母包含在由给定的字符串 - 这样equalsToString:会不会是你想要的。

相反,可能是更好的使用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 
} 

而且,正如帕特里克Goley指出的那样,你需要确保你使用的TextField.text值从它那里得到的字符串。与存储您将用作隐藏词的首字词一样。

也有一些其他次要代码问题(分号在函数头部,例如),你需要清理有一个正常运作的应用程序。

编辑:使字符串调用的范围实际上使用文本字段的文本,并且这样做不区分大小写(防止用户在大写字母为小写字母时放置大写字母,或反之亦然)出现错误返回。还包括链接NSString's rangeOfString:options:

+0

这帮了很多谢谢@Doc。 我唯一的问题是我不知道如何判断这个字母在哪个位置。例如log这个词,有没有办法确定字母o位于这个单词的第二个位置? 谢谢 – HairySocks

+0

绝对。假设rangeOfString:options的返回值:不返回NSNotFound,那么它将返回一个NSRange结构:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/ Reference/reference.html#// apple_ref/doc/c_ref/NSRange 请记住它只会返回搜索词的第一个匹配项。您需要再次运行rangeOfString:options:range:以查看它是否存在于多个位置(其中范围排除了所有内容并包含第一个结果)。 – Doc

1

比较2个字符串:[string1 equalsToString:string2]。如果string1等于string2,这将返回true。获取包含在UITextfield中的字符串:textfield.text

+0

鉴于这是一个hang子手游戏,我假设他正在试图查看给定字符串是否包含字母 - 因此equalsToString:不会是他想要的。相反,使用可能更好鉴于它是一个hang子手游戏,我假设他正在试图查看给定字符串是否包含字母 - 因此equalsToString:不会是他想要的。相反,可能更好使用[string1 rangeOfString:string2] .location!= NSNotFound – Doc

0

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

[self check: self.textfield.text]; 

您还需要创建一个NSString属性从plist中保存您的随机字符串,以便以后可以访问来比较文本框字符串像这样:

声明中在启动方法

@property (nonatomic,strong) NSString* randomString; 

:该类的界面

self.randomString = [swords objectAtIndex:randomIndex]; 

在检查方法:

return [self.randomString isEqualToString:randomString]; 
+0

非常感谢你 – HairySocks

相关问题