Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-30-2009, 06:44 PM   #1 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Question Please Help NSString Problem

Hello,
i am currently making a quiz app and i have a score label which i need to either add 1 for a correct answer or delete one for a incorrect answer. so i have this code

Code:
	result.text = @"Correct";
	
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
This works fine however when i add an if statement for the next question it dosent work. This is the if statement

Code:
if (question.text == @"What was the first programmable computer called?") {
		
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
	
	result.text = @"Incorrect";
wilky94 is offline   Reply With Quote
Old 12-30-2009, 07:05 PM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 534
Default

Hi, First of all, I would keep the score counting seperate from the actual text, it will make it alot easier, 2nd, use NSString's isEqualTo method to compare strings.
QuantumDoja is offline   Reply With Quote
Old 12-30-2009, 07:46 PM   #3 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Quote:
Originally Posted by wilky94 View Post
Hello,
i am currently making a quiz app and i have a score label which i need to either add 1 for a correct answer or delete one for a incorrect answer. so i have this code

Code:
	result.text = @"Correct";
	
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
This works fine however when i add an if statement for the next question it dosent work. This is the if statement

Code:
if (question.text == @"What was the first programmable computer called?") {
		
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
	
	result.text = @"Incorrect";
You can try this:

Code:
if (question.text isEqualToString:@"What was the first programmable computer called?") {
		
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
	
	result.text = @"Incorrect";
}
rocotilos is offline   Reply With Quote
Old 12-31-2009, 07:24 AM   #4 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by rocotilos View Post
You can try this:

Code:
if (question.text isEqualToString:@"What was the first programmable computer called?") {
		
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
	
	result.text = @"Incorrect";
}


Thanks, when i try this i get an error saying "Expected ')' before 'isEqualToString' do you know why this is.
wilky94 is offline   Reply With Quote
Old 12-31-2009, 09:53 AM   #5 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Kyiv, Ukraine
Posts: 27
Default

You should add [], i.e. write
[question.text isEqualToString:@"What was the first programmable computer called?"]
Nikh is offline   Reply With Quote
Old 12-31-2009, 10:56 AM   #6 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Sorry I didnt get back to u earlier.
Nikh got it right. u need to add the [] on it.
It is an evaluating method. So it will return false or true.

Ie,

Code:
if ([question.text isEqualToString:@"What was the first programmable computer called?"])
OR in its more understandable form:

Code:
if ([question.text isEqualToString:@"What was the first programmable computer called?"]==YES)
rocotilos is offline   Reply With Quote
Old 12-31-2009, 11:01 AM   #7 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by rocotilos View Post
Sorry I didnt get back to u earlier.
Nikh got it right. u need to add the [] on it.
It is an evaluating method. So it will return false or true.

Ie,

Code:
if ([question.text isEqualToString:@"What was the first programmable computer called?"])
OR in its more understandable form:

Code:
if ([question.text isEqualToString:@"What was the first programmable computer called?"]==YES)

Its still not working, when i run it the score stays the same do you know what else i can try
wilky94 is offline   Reply With Quote
Old 12-31-2009, 01:21 PM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Fort Lauderdale, Florida
Posts: 67
Default

Quote:
Originally Posted by wilky94 View Post
Its still not working, when i run it the score stays the same do you know what else i can try
Add an NSLog in the if statement to see if it evaluates to true. If it doesn't, NSLog the value of question.text right before the if to see what it is.
__________________


FREE 3D Photo Maker
Ins3rtNam3H3r3 is offline   Reply With Quote
Old 12-31-2009, 04:40 PM   #9 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by Ins3rtNam3H3r3 View Post
Add an NSLog in the if statement to see if it evaluates to true. If it doesn't, NSLog the value of question.text right before the if to see what it is.
i dont understand what you mean could you send me the code i am new to iphone development

Last edited by wilky94; 04-26-2010 at 02:03 PM.
wilky94 is offline   Reply With Quote
Old 12-31-2009, 08:50 PM   #10 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Fort Lauderdale, Florida
Posts: 67
Default

Quote:
Originally Posted by wilky94 View Post
i dont understand what you mean could you send me the code i am a nube to iphone development
I mean you should do:
Code:
if (question.text isEqualToString:@"What was the first programmable computer called?") {
		
        NSLog(@"IF statement true"); 

	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
	
	result.text = @"Incorrect";
}
to check if the IF is true, and if its not you should do:
Code:
NSLog(question.text);

if (question.text isEqualToString:@"What was the first programmable computer called?") {

	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
	
	result.text = @"Incorrect";
}
to see what question.text is and why it's not evaluating to true.
__________________


FREE 3D Photo Maker
Ins3rtNam3H3r3 is offline   Reply With Quote
Old 01-01-2010, 02:06 PM   #11 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Quote:
Originally Posted by wilky94 View Post
Its still not working, when i run it the score stays the same do you know what else i can try
May I know how are you setting the question.text? If there are a space after the ? at the end, this code wont work too. I cant really help u without more details to the codes.
rocotilos is offline   Reply With Quote
Old 01-02-2010, 08:07 AM   #12 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by rocotilos View Post
May I know how are you setting the question.text? If there are a space after the ? at the end, this code wont work too. I cant really help u without more details to the codes.

I have checked that all the questions are the same. this is all the code i have.

Code:
- (IBAction)A:(UIButton *)sender {
    
	result.text = @"Correct";
	
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
	
	NSLog(question.text);
	
	if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
		
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
		
		result.text = @"Incorrect";
	}		
	
	

	
		
			
	
}

- (IBAction)B:(UIButton *)sender {
	
	
	result.text = @"Incorrect";
	
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
	
	
	if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
		
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
		
		result.text = @"Correct";
		
	
		
	}	
}

- (IBAction)C:(UIButton *)sender {
	
	result.text = @"Incorrect";
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
	
	if ([question.text isEqualToString: @"What was the first programmable computer called?"]) {
			
	result.text = @"Incorrect";
		
	
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
		
	}	
    
}

- (IBAction)D:(UIButton *)sender {
	
	result.text = @"Incorrect";
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
	
	if ([question.text isEqualToString: @"What was the first programmable computer called?"]) {
		
		
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
		
		
		result.text = @"Incorrect";
		
		
		
	}	
    
}

- (IBAction)back:(UIButton *)sender {
	
	[self dismissModalViewControllerAnimated:YES];
	}



- (IBAction)next:(UIButton *)sender {
	question.text = @"What was the first programmable computer called?";
	
	answerA.text = @"ZX";
	answerB.text = @"Z1";
	answerC.text = @"ZZ";
	answerD.text = @"Z0";
	number.text = @"2";
wilky94 is offline   Reply With Quote
Old 01-02-2010, 08:26 AM   #13 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Quote:
Originally Posted by wilky94 View Post
I have checked that all the questions are the same. this is all the code i have.

Code:
- (IBAction)A:(UIButton *)sender {
    
	result.text = @"Correct";
	
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
	
	NSLog(question.text);
	
	if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
		
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
		
		result.text = @"Incorrect";
	}		
	
	

	
		
			
	
}

- (IBAction)B:(UIButton *)sender {
	
	
	result.text = @"Incorrect";
	
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
	
	
	if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
		
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
		
		result.text = @"Correct";
		
	
		
	}	
}

- (IBAction)C:(UIButton *)sender {
	
	result.text = @"Incorrect";
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
	
	if ([question.text isEqualToString: @"What was the first programmable computer called?"]) {
			
	result.text = @"Incorrect";
		
	
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
		
	}	
    
}

- (IBAction)D:(UIButton *)sender {
	
	result.text = @"Incorrect";
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
	
	if ([question.text isEqualToString: @"What was the first programmable computer called?"]) {
		
		
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];
		
		
		result.text = @"Incorrect";
		
		
		
	}	
    
}

- (IBAction)back:(UIButton *)sender {
	
	[self dismissModalViewControllerAnimated:YES];
	}



- (IBAction)next:(UIButton *)sender {
	question.text = @"What was the first programmable computer called?";
	
	answerA.text = @"ZX";
	answerB.text = @"Z1";
	answerC.text = @"ZZ";
	answerD.text = @"Z0";
	number.text = @"2";
Hi.. I think I may have guessed at what is wrong (or not wrong rather).

I see you are adding the score before the if statement (see the Red fonts i highlighted above). I think the question is detected, but since u are +1 to the score before the if statement, so after the IF STATEMENT is TRUE, it would -1 to score again which sums up to, 0. So no changes in the score was made.

I may be wrong though.. Is that line correct? Seems a bit weird for me.
rocotilos is offline   Reply With Quote
Old 01-02-2010, 12:05 PM   #14 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by rocotilos View Post
Hi.. I think I may have guessed at what is wrong (or not wrong rather).

I see you are adding the score before the if statement (see the Red fonts i highlighted above). I think the question is detected, but since u are +1 to the score before the if statement, so after the IF STATEMENT is TRUE, it would -1 to score again which sums up to, 0. So no changes in the score was made.

I may be wrong though.. Is that line correct? Seems a bit weird for me.
i dont get errors but i think you are correct do you know what i could do to fix this
wilky94 is offline   Reply With Quote
Old 01-02-2010, 03:56 PM   #15 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Fort Lauderdale, Florida
Posts: 67
Default

Edit - Realized i was completely wrong
__________________


FREE 3D Photo Maker

Last edited by Ins3rtNam3H3r3; 01-03-2010 at 12:49 PM.
Ins3rtNam3H3r3 is offline   Reply With Quote
Old 01-03-2010, 11:25 AM   #16 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Quote:
Originally Posted by wilky94 View Post
i dont get errors but i think you are correct do you know what i could do to fix this
Hi. Maybe u can do it like this:

Code:
- (IBAction)A:(UIButton *)sender {
    
	
	if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
		
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
		
		result.text = @"Incorrect";

                return;  
	}		
	
	result.text = @"Correct";
	
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
	
	NSLog(question.text);
	
}
So if the result is wrong, it would deduct 1 point from score and exit the method (using return). Then all the green code is ignored. If it is right, the if statement is ignored, and the green code is executed. I probably use a complete if..else statement.. but im not sure how ur game work. Give the above code a try.
rocotilos is offline   Reply With Quote
Old 01-04-2010, 06:16 PM   #17 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by rocotilos View Post
Hi. Maybe u can do it like this:

Code:
- (IBAction)A:(UIButton *)sender {
    
	
	if ([question.text isEqualToString:@"What was the first programmable computer called?"]) {
		
		score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] - 1)];	
		
		result.text = @"Incorrect";

                return;  
	}		
	
	result.text = @"Correct";
	
	score.text = [NSString stringWithFormat:@"%i", ([score.text intValue] + 1)];
	
	NSLog(question.text);
	
}
So if the result is wrong, it would deduct 1 point from score and exit the method (using return). Then all the green code is ignored. If it is right, the if statement is ignored, and the green code is executed. I probably use a complete if..else statement.. but im not sure how ur game work. Give the above code a try.
now it is not adding 2 but still adding 1 instead of taking away 1.
wilky94 is offline   Reply With Quote
Old 01-05-2010, 04:25 AM   #18 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Quote:
Originally Posted by wilky94 View Post
now it is not adding 2 but still adding 1 instead of taking away 1.
If it is correct, then u want to add 1 right? and if it is incorrect, subtract 1?

Did u input the "return;"?
rocotilos is offline   Reply With Quote
Old 01-06-2010, 01:28 PM   #19 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by rocotilos View Post
If it is correct, then u want to add 1 right? and if it is incorrect, subtract 1?

Did u input the "return;"?

i must be doing the if statement wrong because now the result.text sais correct but in the if statement it is supposed to say incorrect.
wilky94 is offline   Reply With Quote
Old 04-26-2010, 02:05 PM   #20 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by wilky94 View Post
i must be doing the if statement wrong because now the result.text sais correct but in the if statement it is supposed to say incorrect.
Thanks to everyone. I got it working, what was wrong was I hadn't added one or subtracted 1 on the first question and therefore the others were not working.

thanks again
wilky94 is offline   Reply With Quote
Old 06-19-2010, 08:34 AM   #21 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 1
Default

I am reading data from a TCP/IP stream and am successfully receiving a byte array from the pre-existing server. I am now trying to find a way to convert that array to an NSString.
__________________
220-702 and 350-001 certification guide 640-802 and ccna 640-802
Rosemarry is offline   Reply With Quote
Old 06-19-2010, 09:02 AM   #22 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,526
Default

Quote:
Originally Posted by wilky94 View Post
Thanks to everyone. I got it working,..
But you still have not taken the very important advice of QuantumDoja in the very first response to your posting. You definitely should not use score.text as the primary storage of the value of the score. You will have trouble later on if you don't fix it now. You should store the score in an int value as part of some object other than the view. Perhaps the view controller, or even maybe the app delegate, if the score is essentially a global singleton.

For example, suppose you push another view controller onto the navigation stack and the system decided to unload the score view. That will destroy your one and only memory of the actual score. So when the view needs to be re-created the next time it loads, you won't have any value to put in for the score.

Or, suppose someone is playing your game and they get a phone call, which forces your app to exit. After the call, they return to your app, only to find that their score has disappeared. It is much easier to save the state of an int than to save the state of the text of a view.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Reply

Bookmarks

Tags
nsstring, problem, question, result

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 317
19 members and 298 guests
@sandris, ADY, dacapo, Dani77, djohnson, dre, HDshot, HemiMG, JasonR, MarkC, mer10, nibeck, prchn4christ, ryandb2, spiderguy84, timle8n1, tomtom100, vogueestylee
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:05 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0