Making the app recognize "the right answer" - iphone

I am new in the game, so probably an easy issue. What I am trying to do is to make an app with a question asked, and a textFiled in which to answer. Then, i want the app to recognize when the answer is right (in this case the number 25) and when it is wrong (not 25). Everything is working fine, and I get the "wrong" message, but I cant make it recognize the right answer.
- (IBAction)btnSubmitAction:(id)sender {
if (textFieldAnswer.text == #"25") {
lblAnswer.text = #"Yes, your right!";
btnNext.hidden = 0;
} else {
lblAnswer.text = #"No, try again.";
}
}
Thanks a lot!

Well, this is how you're supposed to compare strings:
- (IBAction)btnSubmitAction:(id)sender {
if ([textFieldAnswer.text isEqualToString:#"25"]) {
lblAnswer.text = #"Yes, you're right!";
btnNext.hidden = NO;
} else {
lblAnswer.text = #"No, try again.";
}
}

Comparing objects in Objective-C is done by ==. This means that you compare pointers, which is not the same as comparing strings, because they are pointers. If you would like to compare strings for equality you should call the BOOL instance method isEqualToString of NSString class.
[string1 isEqualToString:#"someString"]

Related

Swift If program

Still a beginner with just playing around with some basic functions in swift.
Can someone tell me what is wrong with this code?
import UIKit
var guessInt: Int
var randomNum = arc4random_uniform(10)
if Int(randomNum) == guessInt {
println ("correct")
} else {
println; "no, the number is not. guess again"
}
So far the only error I'm getting is that
guessInt
is being used before being initialized!
I've tried to type everything again but still have the same error.
Thanks in advance.
In Swift you cannot read a value before it's set, which you're doing here:
if Int(randomNum) == guessInt
If you change your declaration from this:
var guessInt:Int
to this:
var guessInt = 6
then your code will work as expected (assuming you want the user's guess to be 6).
You've declared guessInt but now you need to initialize, or set it to some initial value.
For example:
let guessInt = 3
Another option is to declare guessInt as an "Optional", meaning that it can be nil, in fact it will be initialized to nil. This will print "no, ...." until you assign guessInit to a non nil value in the range of values produced by arc4random_uniform(10), but it will compile and run cleanly.
var guessInt:Int? // note the ? after Int this marks it as an Optional
var randomNum = arc4random_uniform(10)
if Int(randomNum) == guessInt {
println ("correct")
} else {
println; "no, the number is not. guess again"
}

Can't set a BOOL to yes

I've ran into this problem and so far I can't find any answers. so a have a BOOL property and I am trying to set it to YES/TRUE/true ( I have tried them all) but whenever I check the BOOL it always returns NO/FALSE/false. Below is my code
[myObject setAllowReg:YES];
myObject.allowReg = YES;
if (myObject.allowReg)
{
NSLog(#"YES");
}
else
{
NSLog(#"NO");
}
The output to this is always "NO". This must be something really simple that I am missing but it seems whatever I try doesn't work.
Thanks
Beat guess is that myObject.allowReg is nil and probably myObject is nil.
Add some NSLog statements to help figure this out:
NSLog(#"myObject: %#", myObject);
NSLog(#"myObject.allowReg: %i", myObject.allowReg);
Advice: In debugging break everything down to the simplest statements and test each step along the way.
myObject is probably nil. You can send messages to nil objects, they always return a 0 value.

How to compare strings? (`==` returns wrong value)

I've got myself a piece of the iPhone SDK and been trying to make some simple apps. In this one, i want to compare the first character of self.label.string with the last one of ((UITextField *)sender).text. I decided to name them self.texty and self.input, respectively.
I would expect this if statement returning yes to me under certain circumstances, however I can't seem to get that done.
(in my case, my self.label.string was equal to 'hello!', while my self.input ended in an 'h'.)
self.input = [NSString stringWithFormat:#"%#", [((UITextField *)sender).text substringFromIndex:[((UITextField *)sender).text length]-1]];
self.texty = [NSString stringWithFormat:#"%#", [self.label.string substringToIndex:1]];
if (self.input == self.texty) {
return yes;
} else {
return no;
}
String comparison is not done with ==, but with one of the comparison methods of NSString.
For example:
if ([self.input compare:self.texty] == NSOrderedSame) {
if ([self.input isEqualToString:texty]) {
return yes;
} else {
return no;
}
EDIT:
Or a better version as the commenters noted:
return [self.input isEqualToString:texty];
If you're curious why the == operator doesn't work as expected, it's because you're actually comparing two scalar types (pointers to NSString objects) not the contents of the NSString objects themselves. As a result, it will return false unless the two compared NSStrings are actually the same instance in memory, regardless of the contents.

Trouble comparing NSString to NSArray object

Ok, so I'm trying to check if an object from an NSArray equals something inputed by the User into a UITextField. It should work, but for some reason it dosn't. Here is my code:
if (theAnswer.text == [correctAnswers objectAtIndex:problemNumber]) {
NSLog(#"CORRECT");
}
else {
NSLog(#"wrong");
}
The console always give wrong.
I put this log in:
NSLog(#"%# %#", theAnswer.text, [correctAnswers objectAtIndex:problemNumber]);
And I get A A
wrong
printed everytime. Thanks for the help
Objective-C doesn't support the == operator for NSStrings. That will do a comparison of the pointers to the NSStrings and not the contents of the string itself.
Try
if([theAnswer.text isEqualToString:[correctAnswers objectAtIndex:problemNumber]]) {
}

curious what is wrong with this if statement?

Could someone explain what is wrong with this "if statement"? I get:
"lvalue required as left operand of assignment".
This does not work:
if ([[prepDataArray objectAtIndex:iNDEX] boolValue] = YES) {
NSLog(#"HARD");
}
While this works:
diffQ = [[prepDataArray objectAtIndex:iNDEX] boolValue];
if (diffQ = YES) {
NSLog(#"HARD");
}
I do realize where the problem are and that the 'lvalue' indicate that i need to have something different on the left side but i do not grasp why and how to do what i want inside the 'if' statement as tried in the first example.
Would appreciate if someone nice could give me a hint :-)
if ([[prepDataArray objectAtIndex:iNDEX] boolValue] == YES) {
NSLog(#"HARD");
}
it's == not =
The first one doesn't work because you try to assign a BOOL (YES) to a message. The second one works because you try to assign a BOOL to diffQ. This is correct, but not the result you expect (comparing diffQ to YES)
Common programming error ;) I've done this a millions times
I completely agree with what #thomas said above, but let me add.
Don't compare a bool to YES. It's not that the if construct requires
if( some comparison statement ) {
....
}
That's not the case. The if construct has the following form:
if( a boolean value) {
...
}
It just happens that a comparison statement yields a boolean, so you put that in the if statement.
In your case, boolValue already gives you a bool. You don't need to compare that against YES or NO. That's like doing YES==YES or YES==NO and it's completely redundant.
Just do
if ([[prepDataArray objectAtIndex:iNDEX] boolValue]) {
NSLog(#"HARD");
}