Ios 匹配NSString和UILabel';s文本
我将用户输入的单词与机密单词进行比较。我的Ios 匹配NSString和UILabel';s文本,ios,objective-c,if-statement,nsstring,Ios,Objective C,If Statement,Nsstring,我将用户输入的单词与机密单词进行比较。我的if语句测试正确的单词是否等于猜测的单词是行不通的 @property (weak, nonatomic) IBOutlet UILabel *guesslet; //the guessed letter @property (weak, nonatomic) IBOutlet UILabel *Word; //the ongoing guessed word @property (weak, nonatomic) NSString *wrand;
if
语句测试正确的单词是否等于猜测的单词是行不通的
@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)
@property(弱,非原子)ibuilabel*guesslet//猜到的信
@属性(弱,非原子)IBUILabel*字//正在猜测的单词
@属性(弱,非原子)NSString*wrand//正确的词
@属性(弱,非原子)NSString*letterguess;
-(iAction)猜测按钮//猜单词的按钮
-(iAction)猜测按钮
{
letterguess=guesslet.text;
布尔匹配=否;
char charInput=[guesslet.text characterAtIndex:0];
对于(NSInteger i=0;i
我做错了什么?使用==进行比较将进行指针(或引用)比较,如果要比较实际的NSString,则需要使用
if([Word.text isEqualToString:randword]) {
}
使用==进行比较将进行指针(或引用)比较,如果要比较实际的NSString,则需要使用
if([Word.text isEqualToString:randword]) {
}
=
是赋值,=
是平等性测试,但是因为您使用的是Objective-C,所以您有引用,所以您是在比较引用,而不是那些引用的内容。您需要使用[Word.text isEqualToString:randword]
。如果您刚开始iOS开发,我强烈建议您使用Swift而不是客观的Calso,randword
randword来自哪里?您没有定义它,我想您是在尝试做[Word.text IsequalString:wrand]
=
是赋值,=
是平等性测试,但因为您使用的是Objective-C,所以您有引用,所以您是在比较引用,而不是这些引用的内容。您需要使用[Word.text isEqualToString:randword]
。如果您刚刚开始iOS开发,我强烈建议您使用Swift而不是客观的Calso。那么randword是从哪里来的?您没有定义它,我想您是在尝试做[Word.text IsequalString:wrand]