开发者

Something really dumb with return values

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...


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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜