views:

193

answers:

2

I am having a really odd problem trying to set a simple float value to 1.

My property:

{
float direction;
}
@property(nonatomic)float direction;

Which is synthesized:

@synthesize direction;

I then used the following code:

- (void)setDirection:(float)_direction {
    NSLog(@"Setter called with value of %f",_direction);
    self->direction = _direction;
}

For testing purposes...

When I try to change the value with this,

[[w getCharacter] setDirection:1.0f];

where [w getCharacter] gives this:

return [[[self scene] gameLayer] player];

I get the warning, "setDirection not defined."

If I switch to dot notation([w getCharacter].direction), I get "confused by earlier errors, bailing out".

Here is where the weirdness starts. When I try to change the value, the debug message displays _direction = 0.00000. When I check the number later, its still 0.000. I am clearly trying to change it to 1, why is this not working?

A: 
justin
luke does NOT have a recursive loop. His setter is using "self->direction" which is already the same as just "direction" and not the same as the incorrect "self.direction".
gerry3
If I switch to dot notation([w getCharacter].direction), I get "confused by earlier errors, bailing out".is what caused me to go looking at that. It compiles for me, but acts as I would expect.
justin
oh, you're correct. The problem exists before this.
justin
Hey, just tried switching to what justin said, still same problem.If it were recursive, Wouldn't the log just log away?Any ideas where I should look for the problem? Its odd because the method is being called,setDirection I mean. I know this from the log message. But why would the parameter change getting there?If it helps, all my objective-c code is being compiled as c++.(.mm file extension).Thanks for the help.
luke
Yeah, um, my answer was profoundly stupid and should be ignored (well, it's true; it just has nothing to do with your question. I'd delete it but I don't think I'm allowed to. I mis-read your post before it was reformated.
justin
NO problem what so ever. Thanks for replying. It brought up a very interesting point about the loop. Never thought of that. And in no way was it stupid.
luke
+1  A: 

The simplest explanation is that [w getCharacter] doesn't return the class of object you think it does. Only the class that has direction defined for it can respond to the message. You should test this by explicitly calling it with the class it defined for.

It is possible you did not include the header that defines the method.

Two probably unrelated issues:

The self->direction construction will work for a scalar value but it does an end run around the entire class concept. In this case just use: 'direction=_direction;` and it will set it directly.

Apple reserves all names that start with underscores for its own internal use. You should not use them because Objective-c has a global name space. It's possible that you can accidentally use an Apple variable that is defined deep within a framework. (This is why framework constants all start with NS,CF,CA etc.)

TechZen
Many thanks, This solved it. It was header stuff. I did not include any of the headers I needed. I didn't realize that this bug could be from lack of headers. I just assumed there was other weird-ness going on.As for _direction. That's one bug I hope I never have to battle with.Thanks again for minimal hair pulling out.
luke