views:

73

answers:

1

Hello everyone, first post here so sorry for the length of it. I've been lurking and learning a lot so far but now I have to step in and ask a question. I have read numerous posts here as advised in the FAQs, but I couldn’t find exactly the answer I’m looking for.

Before anything else, let me just say that I'm a total beginner in programming (let alone Objective-C) so please excuse me for any misuse of the terminology. Same goes for any funny english as english not my native language.

I'm building an unit conversion application with a main window containing (among other stuff) two popUpButtons. I'm using indexOfSelectedItem from both popUpButtons in order to calculate a float value (I'm getting the indexes initially in the AwakeFromNib and later in the pop up buttons controller method, when the user change selection).

My problem consists of two parts: first, the code for calculation of that float is pretty massive as I'm comparing every combination of the two indexes of selected items. And second, I would need to have the calculated float value available immediately after launch as the user might want to perform a conversion before using any of the window popUpButtons (otherwise I would put the calculation code in a -(IBAction) method).

So, I'm trying with the following code for calculation of the float value:

@interface MyClass: NSObject

float calculatedFloat;

-(void)setCalculatedFloat:(float)calcFl;
-(float)calculatedFloat;

@implementation MyClass

-(void)setCalculatedFloat:(float)calcFl {
  calcFl = 1.0; // I'm simplifying, this is where I'd like to perform calculation
}

-(float)calculatedFloat {
  return calculatedFloat;
}

Now, for the first part of my problem, when I use the calculatedFloat in another method, say:

-(void)printIt {
  NSLog(@"Calculated float equals: %.2f", calulatedFloat); 
}

all I receive in Debugger is 0.00.

First question would be: if this is not working, how do I properly access this value from within another method?

For the second part of the problem, I'm using -(void)AwakeFromNib; to set up popUpButtons etc. right after the launch but I really wouldn't like to put all of the float calculation code in it only to repeat it somewhere else later.

So the second question would be: is this even possible what I'm trying to achieve? Further more, do I need to move this calculation code to another class? If so, how can I make that other class aware of the indexOfSelectedItem from a popUpButton?

Sorry for the lengthy post and possibly confusing and silly questions. I hope you didn't have to cringe your teeth too much while reading! :)

Thanks!

+1  A: 
-(void)setCalculatedFloat:(float)calcFl {
    calcFl = 1.0; // I'm simplifying, this is where I'd like to perform calculation
}

This doesn't show up when you print it later because you assigned to the variable holding the new value, not the variable for the value of the property. You need to assign to your calulatedFloat instance variable. (You typo'ed that variable name, BTW.)

You should move the calculating into another method, and send yourself that message from awakeFromNib and from anywhere that needs to cause recalculation. That method should call setCalculatedFloat: with the calculated value—i.e., setCalculatedFloat: should be just a simple setter. Once you make that change, you could replace your custom accessors with a @synthesize statement and let the compiler write the accessors for you.

My problem consists of two parts: first, the code for calculation of that float is pretty massive as I'm comparing every combination of the two indexes of selected items.

You might see whether you can create custom objects to set as the menu items' representedObject properties, in order to cut out this massive comparison tree. It's hard to be more specific about this without knowing what your comparison tree does.

Peter Hosey
Thank you very much for your prompt reply. I’ve been trying to figure out the "You should move the calculating into another method (…). That method should call setCalculatedFloat:(…)” part but unfortunatelly it seems that this is exactly the part I do not get.Eventually, this is all I could I came up with:@implementation MyClass-(void)setCalculatedFloat:(float)calcFl { calculatedFloat = calcFl; }-(float)calculatedFloat { return calculatedFloat; }-(float)calculateTheFloat { float abc = 1.0; // [self setCalculatedFloat:abc];}And the warning for the 3rd method, obviously.
Miloš
`calculateTheFloat` shouldn't return anything; it should send the `setCalculatedFloat:` message, exactly as you commented out.
Peter Hosey
Unfortynately that comment got garbled into one line. Line `[self setCalculatedFloat:abc];` is not commented, empty comment is after the previous line. With `[self setCalculatedFloat:abc];` I get the `warning: control reaches end of non-void function`" and `NSLog` still returns `calculatedFloat = 0.00`. Thanks one more time.
Miloš
learn more Objective-C basics you must
Sending the `setCalculatedFloat:` message isn't what got you that warning; not returning a value from a method that you declared as returning a (`float`) value is what got you that warning. You need to declare the method as not returning a value (declare its return type as `void`) and send the `setCalculatedFloat:` message.
Peter Hosey
Yes, that’s what I thought at one moment. So I wrote this: `-(void)calculateTheFloat:(float)someTemp { someTemp = 123.4; [self setCalculatedFloat:someTemp]; }` but the result was the same (minus the warning) :( That’s what got me so confused… @ram-ram, I obviously do not understand this good enough but you have to trust me that I wouldn’t bother you guys if I only could find the answer elsewhere. All of the "for the beginners” titles go into great details of more complex stuff simply leaving things like this to be learned elsewhere…
Miloš
Are you sure you're sending `calculateTheFloat:` before `printIt`? Are you sure you're sending both messages to the same object? Are you sure you're not zeroing out the property somehow in between?
Peter Hosey
Well, it must be that I am zeroing it out, it’s just that I cannot understand where and how. I just made new clean project with only one class, those three methods in it and one button in the window (`(IBAction)` method for `NSLog(@"%.2f", calculatedFloat)`). When I click the button — i get the `0.00` printout. So confused right now… I had no trouble with the popUpButtons controls but I really got stuck in this. Here it is, after all: http://bit.ly/c8MdHh (57 kB) so if you could perhaps look into it it would be great help.
Miloš
I’ve been banging my head with this for two days straight and I ain’t gonna let it go until I figure out what is happening here :) OK, in order to better understand this, I commented out the `calculateTheFloat` method and I moved assignement and method call to the `(IBOutlet)printIt:` method. Now it works. But like i said, i have far, far more calculation code than this and I really wouldn’t like to keep it inside the `IBOutlet` method. Is that even possible? Can someone explain to me how to achieve this method call from another method (say, `calculateTheFloat`)? Thanks! http://bit.ly/aNX6rF
Miloš
OK, finally got it (I do hope so!). The solution, for the next newbie, is in the link below. Peter, thank you very much for your help. Things you wrote made me think about what I was doing. Experienced programmers often forget how much trouble beginners have with the terminology, let alone everything else. You’ve been great help! And for the next guy who is writing the "For the beginners" book: skip the parts on the origins of the "NS" prefix and focus more on stuff like this. And the link: http://bit.ly/9hKKc3
Miloš
Miloš: Yup—if `prinIt:` is an action, then that's pretty much it (although I might rename the action at this point; something like “`calculateAndPrintResult:`”). Based on what you showed in your question, I'd thought it was a regular method and that you were calling each of `calculateTheFloat` and `printIt` yourself from other places in your code.
Peter Hosey