Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JDSFTW

macrumors newbie
Original poster
Oct 14, 2011
5
0
Hello MacRumors,

I'm new here and just started obj - C programming last week. Today I was performing the ritual of trying to create a calculator app using the xcode and userinterface.

I finished but when I debugged, I got some messages that I didn't quite understand.

It was saying that some of my methods were undeclared, which doesn't make sense since I defined them in my @interface section...

Anyways, here's the code

Code:
#import "Calculator4_vbaViewController.h"

@implementation Calculator4_vbaViewController
-(CalculatorBrain *)brain
{
	if (!brain) {
		brain = [[CalculatorBrain alloc] init];
	}
	return brain;
-(IBAction)digitPressed:(UIButton *)sender;
{
	if (userIsInTheMiddleOfTypingANumber) {
		[display setText: [[display text] stringByAppendingString:digit]];
	} else {
		[display setText:digit];
		userIsInTheMiddleOfTypingANumber = YES;
	}
}
-(IBAction)operationPressed:(UIButton *)sender;
{
	NSString *digit = [[sender titleLabel] text];
	if (userIsInTheMiddleOfTypingANumber) {
		[[self brain] setOperand:[[display text] doubleValue]];
		userIsInTheMiddleOfTypingANumber = NO;
	}
		
	NSString *operation = [[sender titleLabel] text];
	double result = [[self brain] performOperation:operation];
	[display setText: [NSString stringWithFormat:@"%g", result]];
}

@end
 
Last edited by a moderator:
Post the actual text of the actual error messages.

This code:
Code:
@implementation Calculator4_vbaViewController
-(CalculatorBrain *)brain
{
if (!brain) {
brain = [[CalculatorBrain alloc] init];
}
return brain;
[COLOR="Red"]// missing a } here[/COLOR]
has the problem hilited in red.
 
My first reading revealed some syntax errors in your code. Blue code needs to be added, red code needs to be removed.

Code:
#import "Calculator4_vbaViewController.h"

@implementation Calculator4_vbaViewController
-(CalculatorBrain *)brain
{
	if (!brain) {
		brain = [[CalculatorBrain alloc] init];
	}
	return brain;
[COLOR="Blue"]}[/COLOR]
-(IBAction)digitPressed:(UIButton *)sender[COLOR="Red"];[/COLOR]
{
	if (userIsInTheMiddleOfTypingANumber) {
		[display setText: [[display text] stringByAppendingString:digit]];
	} else {
		[display setText:digit];
		userIsInTheMiddleOfTypingANumber = YES;
	}
}
-(IBAction)operationPressed:(UIButton *)sender[COLOR="Red"];[/COLOR]
{
	NSString *digit = [[sender titleLabel] text];
	if (userIsInTheMiddleOfTypingANumber) {
		[[self brain] setOperand:[[display text] doubleValue]];
		userIsInTheMiddleOfTypingANumber = NO;
	}
		
	NSString *operation = [[sender titleLabel] text];
	double result = [[self brain] performOperation:operation];
	[display setText: [NSString stringWithFormat:@"%g", result]];
}

@end
 
Update on corrections

ok so I did exactly what you two said and now I only have 1 error as opposed to 8 or something I had earlier.

It is still saying 'digit' undeclared in this line of code:


if (userIsInTheMiddleOfTypingANumber) {
[display setText: [[display text] stringByAppendingString:digit]];

Thanks for your guys's help I just started programming and wanted to get my feet wet with the calculator app.
Code:
#import "Calculator4_vbaViewController.h"

@implementation Calculator4_vbaViewController
-(CalculatorBrain *)brain
{
	if (!brain) {
		brain = [[CalculatorBrain alloc] init];
	}
	return brain;
}
-(IBAction)digitPressed:(UIButton *)sender
{
	if (userIsInTheMiddleOfTypingANumber) {
		[display setText: [[display text] stringByAppendingString:digit]];
	} else {
		[display setText:digit];
		userIsInTheMiddleOfTypingANumber = YES;
	}
}
-(IBAction)operationPressed:(UIButton *)sender
{
	NSString *digit = [[sender titleLabel] text];
	if (userIsInTheMiddleOfTypingANumber) {
		[[self brain] setOperand:[[display text] doubleValue]];
		userIsInTheMiddleOfTypingANumber = NO;
	}
		
	NSString *operation = [[sender titleLabel] text];
	double result = [[self brain] performOperation:operation];
	[display setText: [NSString stringWithFormat:@"%g", result]];
}

@end
 
Last edited by a moderator:
1. Please use CODE tags around your code. Read this:
https://forums.macrumors.com/threads/747660/

It's from the Sticky at the top of the iPhone/iPad Programming forum.
The Sticky is labeled "Sticky: Posting code, please use the
Code:
 tags"


2. Post the contents of "Calculator4_vbaViewController.h".


3. In your code:
[CODE]
[COLOR="Red"]-(IBAction)digitPressed:(UIButton *)sender
{
	if (userIsInTheMiddleOfTypingANumber) {
		[display setText: [[display text] stringByAppendingString:digit]];
	} else {
		[display setText:digit];
		userIsInTheMiddleOfTypingANumber = YES;
	}
}
[/COLOR][COLOR="Blue"]-(IBAction)operationPressed:(UIButton *)sender
{
	NSString *digit = [[sender titleLabel] text];
	if (userIsInTheMiddleOfTypingANumber) {
		[[self brain] setOperand:[[display text] doubleValue]];
		userIsInTheMiddleOfTypingANumber = NO;
	}
		
	NSString *operation = [[sender titleLabel] text];
	double result = [[self brain] performOperation:operation];
	[display setText: [NSString stringWithFormat:@"%g", result]];
}
[/COLOR]
There is no variable named 'digit' in the body of the method hilited in red. There is a 'digit' in the body of the method hilited in blue. Unfortunately, that digit is not accessible from the red method. If you don't understand why that's so, you need to review how variables are declared, and how a variable defined in one block is not accessible to blocks declared outside it.

Overall, you should probably spend more time reviewing how to write code properly. If the Calculator is your very first app, and you haven't completed any tutorial walk-through on how to write code, then you're already in over your head. It might help if you told us what tutorials or books you've gone through to reach this point.
 
Last edited:
1. Please use CODE tags around your code. Read this:
https://forums.macrumors.com/threads/747660/

It's from the Sticky at the top of the iPhone/iPad Programming forum.
The Sticky is labeled "Sticky: Posting code, please use the
Code:
 tags"


2. Post the contents of "Calculator4_vbaViewController.h".


3. In your code:
[CODE]
[COLOR="Red"]-(IBAction)digitPressed:(UIButton *)sender
{
	if (userIsInTheMiddleOfTypingANumber) {
		[display setText: [[display text] stringByAppendingString:digit]];
	} else {
		[display setText:digit];
		userIsInTheMiddleOfTypingANumber = YES;
	}
}
[/COLOR][COLOR="Blue"]-(IBAction)operationPressed:(UIButton *)sender
{
	NSString *digit = [[sender titleLabel] text];
	if (userIsInTheMiddleOfTypingANumber) {
		[[self brain] setOperand:[[display text] doubleValue]];
		userIsInTheMiddleOfTypingANumber = NO;
	}
		
	NSString *operation = [[sender titleLabel] text];
	double result = [[self brain] performOperation:operation];
	[display setText: [NSString stringWithFormat:@"%g", result]];
}
[/COLOR]
There is no variable named 'digit' in the body of the method hilited in red. There is a 'digit' in the body of the method hilited in blue. Unfortunately, that digit is not accessible from the red method. If you don't understand why that's so, you need to review how variables are declared, and how a variable defined in one block is not accessible to blocks declared outside it.

Overall, you should probably spend more time reviewing how to write code properly. If the Calculator is your very first app, and you haven't completed any tutorial walk-through on how to write code, then you're already in over your head. It might help if you told us what tutorials or books you've gone through to reach this point.


Yeah ok so like I said I am brand new at programming

I am doing the stanford University iOS programming lectures on Itunes U but more so I am reading and doing the exercises of the Kochan book that is highly recommended.

I acknowledge that I am in over my head, but it is important to start getting hands on experience with code and not just try to bookworm it. I'm pretty happy actually that I've done as much as I've done for only doing it for around a week.

I know I have to define the variable, but what do I define it as?


and I apologize about the code tags, I've tried to use them multiple times but it doesn't seem to work with the (#) method.
 
I acknowledge that I am in over my head, but it is important to start getting hands on experience with code and not just try to bookworm it.
Objectively speaking, that's the opinion of a person with only one week of programming experience.

I know I have to define the variable, but what do I define it as?
You already defined it once. If you understand that code, then you should know what to do. If you don't understand that code, then it's time to bookworm it.

and I apologize about the code tags, I've tried to use them multiple times but it doesn't seem to work with the (#) method.
There's no way anyone can debug that. You haven't described a procedure. You've simply given an opinion of an outcome: "It doesn't seem to work". You may also have described what you did, "the (#) method", but it's not at all clear what "the (#) method" means.

To debug something effectively, you should:
1. Be specific.
2. Post your code (or other procedure).
3. Describe what you expected to happen.
4. Describe what actually happened.

So exactly what procedure did you follow? Exactly what actions did you perform? Post an example, describing what you did and what happened.

My example:

1. I typed the sentence: This is an example of CODE tags.
2. I selected the sentence.
3. I clicked the # icon-button, i.e. the CODE tag button.
4. I observed that the sentence was surrounded by CODE and /CODE tags enclosed in []s.
5. I clicked the "Preview Post" button to preview my post.
6. The sentence shows up as a CODE section.
Here it is:
Code:
This is an example of CODE tags.

Post your example.
 
Ok?

"Objectively speaking, that's the opinion of a person with only one week of programming experience."

Yeah obviously its an opinion. Just like it's your opinion that I go back to reading the book. Hands on is a better method for me to learn. Bookworming it may be better for you. Either way, you sound a little bitter at the world. Instead of actually helping with the code, you seem excited to try and point out that I don't have programming experience, which I already pointed out.


You already defined it once. If you understand that code, then you should know what to do. If you don't understand that code, then it's time to bookworm it.
Again, you are pointing out the obvious, I got as far as I've gotten and only had a few errors, I'm pretty proud of myself for only have been doing it a little over a week; but I'm sure you were much better when you first started programming...

There's no way anyone can debug that. You haven't described a procedure. You've simply given an opinion of an outcome: "It doesn't seem to work". You may also have described what you did, "the (#) method", but it's not at all clear what "the (#) method" means.

To debug something effectively, you should:
1. Be specific.
2. Post your code (or other procedure).
3. Describe what you expected to happen.
4. Describe what actually happened.

So exactly what procedure did you follow? Exactly what actions did you perform? Post an example, describing what you did and what happened.

My example:

1. I typed the sentence: This is an example of CODE tags.
2. I selected the sentence.
3. I clicked the # icon-button, i.e. the CODE tag button.
4. I observed that the sentence was surrounded by CODE and /CODE tags enclosed in []s.
5. I clicked the "Preview Post" button to preview my post.
6. The sentence shows up as a CODE section.
Here it is:
Code:
This is an example of CODE tags.

Post your example.

Nevertheless I fixed it so all is resolved
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.