views:

97

answers:

2

I'm doing something really dumb, and I don't see it.

I've got an object doc with a method:

-(float) currentOrient
{
    return 50.5;
}

In another object, I call:

-(void) showPage
{

    float rot2=0;
    rot2 = [doc currentOrient] ;
    NSLog(@"SP rotation is %.2f", rot2);
}

However, the output is :

SP rotation is 1112145920.000000

No, one question is "Why is the %2f not formatting correctly?" But the more confusing question is "Where is that number coming from?" Yes, I've walked through it with a debugger, the value of rot DOES change from the garbage it starts with. and that number DOES appear to be consistent.

Clearly something really dumb is going on...

+1  A: 

I can answer the first question:

Why is the %2f not formatting correctly?

Because it ought to be %1.2f to round to two decimal places (which I believe is what you're trying to achieve?)

And guess at the second:

Do you have a property named rot in the code? Other than that... shrug... I don't know - I'm assuming you've simplified the example to post on SO, have you taken out other code that may be relevant? Based on the information you've provided everything should be ducky.

On a side note: When I hit bugs like this I go do something physical. Usually when I come back my head is clear and I find the problem immediately. You might want to give that a try too! :D

Gavin Miller
1st question: DUH. yep. thanks.2nd question. No, there's no property... And I copied the code out of the actual program (I simplified it in the program, for debugging, but that is the current state...) and I don't think that there's anything else happening that might be intefering... Full version of the 2 methods is posted.
Brian Postow
Where are you getting `doc` from? If it's a property you should be accessing it with `self.doc` not the variable.
Gavin Miller
doc is just a member field in the (in this case) AppController object. It *IS* calling the currentOrient method. I traced through it with the debugger... And yeah, I think I should do something physical, like go home, have some dinner, go to sleep and come back in tomorrow B-)
Brian Postow
+1  A: 

It sounds like the showPage method doesn't know right return type for currentOrient, so it's interpreting the value returned as an int and casting that nonsensical int to a float. Are you getting any warnings? Are you sure you're importing the header for currentOrient correctly? Is the currentOrient method declared correctly?

Chuck
The default return type for unknown methods is `id`, not `int`.
dreamlax
Yes, but there's any number of reasons why it might be thinking "int" here (to name just one, there could be a method with the same name and `int` return type in another class), and a mixed up return type is definitely what it sounds like is happening. If it were assuming `id`, he'd be getting an error since that type is incompatible with floats.
Chuck
Sorry, I was referring to your statement "`showPage` method doesn't know the return type of `currentOrient`, so it's interpreting the value returned as an int". If the method *truly* didn't know the return type, it would be assuming `id`, not `int`.
dreamlax
no, currentOrient is declared as -(float) currentOrient; and it is included.
Brian Postow
Err, nope. on second thought, no it wasn't. I changed the .h file, ANDTHEN FORGOT TO SAVE IT. D'OH
Brian Postow