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

JoshAK

macrumors newbie
Original poster
Aug 12, 2008
19
0
Yes I'm a newbie to cocoa. I can create calculations in xcode for my desktop...but when I try to add two fields on the iPhone I'm unsuccessful. Can someone give me a basic idea on how to accomplish this simple task. This is how I would do it for my desktop:

float num1, num2, answer1, answer2;
num1 = [price floatValue];
num2 = [dollar floatValue];
answer1 = num1 * (num2 / 100);

[resLabel setFloatValue:answer1];

I'm not sure how to translate that to the iPhone. Any help would be much appreciated. Thanks,

Josh
 

grimjim

macrumors member
May 24, 2003
75
0
I don't think that UILabel has a setFloatValue: method. Instead, it has a text property that either sets the label's text from an NSString that gets passed to it, or returns an NSString representing the current value of the label's text.
So, you need to convert your float to an NSString first, and then use it to set the UILabel's text property.

Here's the quick and easy way:

Code:
float num1, num2, answer1, answer2;
num1 = [price floatValue];
num2 = [dollar floatValue];
answer1 = num1 * (num2 / 100);

NSString *answerString = [NSString stringWithFormat:@"%d", answer1];
resLabel.text = answerString;

Alternatively, I suppose you could do something like:

Code:
float num1, num2, answer1, answer2;
num1 = [price floatValue];
num2 = [dollar floatValue];
answer1 = num1 * (num2 / 100);

NSNumber *theAnswer = [NSNumber numberWithFloat:answer1];
resLabel.text = [theAnswer stringValue];

Whichever seems neater for you.
 

JoshAK

macrumors newbie
Original poster
Aug 12, 2008
19
0
Thanks for the help. I've re-worked some of my code. I'm still having a little issue. The following is my code:

Code:
        NSNumber *price1 = price.text;
	NSNumber *dollar1 = dollar.text;
	
	int string1 = [price1 intValue] / [dollar1 intValue];
	NSString *productString = [NSString stringWithFormat: @"%d", string1];
	//[product setText:productString];
	totalPrice.text = productString;

The trouble is a warning that says: "warning: initialization from distinct Objective-C type"

I understand that this warning is saying my code should look like this:

Code:
NSNumber *price1 = [[NSNUMBER alloc] initWithFromat:@"%d", price];

But that brings other problems. I can't seem to make this bug free. Can someone clear this up for me. Thanks,

Josh
 

grimjim

macrumors member
May 24, 2003
75
0
Almost there...

You've almost got it.

What the warning is actually saying is that you are trying to initialise your NSNumber objects by saying that they are string objects. If you think about it, that doesn't make sense.

NSNumber doesn't have a numberWithString: class method, so you can't use an NSString to initialise it. However, NSString does have a floatValue: method, so you can convert the string to a float, and then use that to initialise the NSNumber. Like this:

Code:
NSNumber *price1 = [NSNumber numberWithFloat:[price.text floatValue]];
NSNumber *dollar1 = [NSNumber numberWithFloat:[dollar.text floatValue]];

If you change your two NSNumber declarations to the above code, the rest of the code you supplied previously should work.
But... note that I have used floats rather than ints. If you're dealing with money, and I'm assuming that you are, you will probably need to use floats to capture cents as well as dollars. You will also need to change the type of string1 to float, and get the floatValues of price1 and dollar1 when you come to do the calculation. This might save you a bit of head-scratching in the future. And I suggest that you start looking through the documentation for ways to format the output to look more like currency values. NSNumber has a method called descriptionWithLocale: that may help you.

And finally, just a little bit of general advice about style: be careful with your variable names. Calling variables price, price1, and so on is OK in a short piece of code, but as you get further into your program, are you going to remember the difference between price8 and price9? I know I wouldn't. And also, you have an integer called string1. When you look back over the code, what type are you going to assume it is? An integer, or a string? I'm certainly not trying to get at you, but it's a good idea to get into good habits as soon as possible. I didn't, and it was painful for a while. But when you're poring over your code in a week's time, thinking, "Why the &%*@ isn't it working now?", having variables whose names reflect their functions will really help. Take it from one who found out the hard way. :D
Have fun, now!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.