Objective c 在从NSArray获得值后,一直在让程序继续运行
我对目标c非常陌生,当我陷入困境时,我会慢慢地用谷歌等工具学习,但我已经被困了好几个小时了。 我正在做一个基本的测验,答案是数字。 我有两个数组的问题和答案,我得到了要索引在一起的数组,NSlog显示它们是正确的,但是如果选择了正确的答案,我的代码就是不同意。 答案总是错的,即使你说它是对的。 谢谢你的帮助Objective c 在从NSArray获得值后,一直在让程序继续运行,objective-c,nsarray,Objective C,Nsarray,我对目标c非常陌生,当我陷入困境时,我会慢慢地用谷歌等工具学习,但我已经被困了好几个小时了。 我正在做一个基本的测验,答案是数字。 我有两个数组的问题和答案,我得到了要索引在一起的数组,NSlog显示它们是正确的,但是如果选择了正确的答案,我的代码就是不同意。 答案总是错的,即使你说它是对的。 谢谢你的帮助 _arrayQuestions = [[NSArray alloc] initWithObjects: @"what is johns age?",
_arrayQuestions = [[NSArray alloc] initWithObjects:
@"what is johns age?",
@"What is daves age ",
nil];
_arrayAnswers = [[NSArray alloc] initWithObjects:
@"32",@"42",
nil];
- (void)setQuestions {
currentQuestionIndex++;
if (currentQuestionIndex == [_arrayQuestions count]) {
currentQuestionIndex = 0;
}
NSString *question = [_arrayQuestions objectAtIndex:currentQuestionIndex];
NSLog(@"displaying question: %@", question);
[_questionLabel setText:question];
// NSLog(@"current answers: %@",[ _arrayAnswers objectAtIndex:currentQuestionIndex]);
}
- (void)checkingAnswers {
// usersAnswer = _DateField.text ;
answer = [_arrayAnswers objectAtIndex:currentQuestionIndex];
// int intAnswer = [answer intValue];
// NSLog(@"answer = : %@", usersAnswer );
NSLog(@"answer = : %@", _DateField.text);
NSLog(@"answer = : %@", answer);
if ( _DateField.text == answer) {
NSString *rightAnswer = [[NSString alloc] initWithFormat:@"Correct" ];
[_answerLabel setText : rightAnswer];
} else {
NSString *wrongAnswer = [[NSString alloc] initWithFormat:@"Wrong" ];
if ( [_DateField.text length] ==2 ) {
[_answerLabel setText : wrongAnswer];
}
}
- (IBAction)numbersPressed:(UIButton *)sender {
if ( [_DateField.text length] <=1) {
NSString *number = sender.currentTitle;
_DateField.text = [ _DateField.text stringByAppendingString: number];
[self checkingAnswers];
}
}
\u arrayQuestions=[[NSArray alloc]initWithObjects:
@“约翰的年龄是多少?”,
@“什么是戴维时代”,
零];
_arrayAnswers=[[NSArray alloc]initWithObjects:
@"32",@"42",
零];
-(无效)设置问题{
currentQuestionIndex++;
如果(currentQuestionIndex=[[u arrayQuestions count]){
currentQuestionIndex=0;
}
NSString*question=[\u arrayQuestions对象索引:currentQuestionIndex];
NSLog(@“显示问题:%@”,问题);
[_questionlabelsettext:question];
//NSLog(@“当前答案:%@,[\u arrayAnswers objectAtIndex:currentQuestionIndex]);
}
-(无效)检查答案{
//usersAnswer=_DateField.text;
答案=[[u arrayAnswers objectAtIndex:currentQuestionIndex];
//int intAnswer=[回答intValue];
//NSLog(@“answer=:%@),usersAnswer);
NSLog(@“answer=:%@),_DateField.text);
NSLog(@“答案=:%@”,答案);
如果(_DateField.text==答案){
NSString*rightAnswer=[[NSString alloc]initWithFormat:@“Correct”];
[_answerlabelsettext:right-answer];
}否则{
NSString*ErrorAnswer=[[NSString alloc]initWithFormat:@“错误”];
如果([\u DateField.text length]==2){
[_answerlabelsettext:错误答案];
}
}
-(iAction)数字按下:(UIButton*)发送器{
如果([\u DateField.text length]不使用“==”,请使用isEqualToString:。“==”正在测试地址的相等性,而不是对象本身。这两个NSString没有相同的地址,因为它们是具有相同值的不同NSString(一个是您的正确答案,另一个是用户提交的答案).Objective-C对象和引用与好的ol'C原语之间有着重要的区别
// C
int a = 2;
if (a == 2)
{
do_something();
}
前面的代码有效,因为=
运算符检查变量a
中包含的值
另一方面,如果您执行以下操作:
// Objective-C
NSString *str1 = @"Hello";
if (str1 == @"Hello")
{
[self doSomething]
}
str1
现在保存一个Obj-C对象的内存地址,该对象保存字符串“Hello”的每个字符。如果要用NSLog(@“%p”,str1)
打印它,它可能会显示一个十六进制地址,类似于0xC35AF3
话虽如此,上面的代码将不起作用,因为=
并不是比较变量的真正内容,而是比较变量的地址,因为这就是str1
所持有的,一个依赖于体系结构的地址指针。从这个意义上说,操作符=
只会检查ring是完全相同的字符串,如中所示,它们都指向内存中的同一字符串
您的代码正在执行类似的操作,但只有Obj-C字符串存储在数组中,您只需使用用户的输入检查它
在这种情况下,使用方法[stringA isEqualToString:stringB]
,该方法在Obj-C字符串内逐个字符进行检查
在使用ObjC之前,我建议您牢牢掌握C编程的基本原理,因为您将比您想象的更多地处理这些基本原理,特别是如果您来自一种解释性的不考虑记忆语言。请查看K&R C和手册页。也许您的意思是:
- (IBAction)numbersPressed:(UIButton *)sender
{
_DateField.text = sender.currentTitle;
[self checkingAnswers];
}
而不是
- (IBAction)numbersPressed:(UIButton *)sender {
if ( [_DateField.text length] <=1) {
NSString *number = sender.currentTitle;
_DateField.text = [ _DateField.text stringByAppendingString: number];
[self checkingAnswers];
}
-(iAction)数字按下:(UIButton*)发送器{
if([\u DateField.text length]不是Xcode问题。与其检查if(\u DateField.text==answer),不如用if([\u DateField.text IsequalString:answer]试试)非常感谢你,真不敢相信我花了这么长时间在这上面。我现在需要弄清楚如何继续下一个问题,干得好,我今天没有工作。谢谢。但我的答案只有在应用@Ben Pious答案时才有效