views:

98

answers:

3

I have a view controller that is created by the app delegate - it's the first one shown in the app.

In its interface I declare

float lengthOfTime;

I also set it as a property:

@property (nonatomic) float lengthOfTime;

And in it's implemetation:

@synthesize lengthOfTime;

In the class viewDidLoad method, I set the value:

self.lengthOfTime = 3.0f;

However, after this, the value is always zero.

No errors, no compile warnings, nothing. Just zero.

The class is instantiated, it is showing in the view, so I'm pretty sure it's not a nil reference.

I've searched all over Google and can't figure it out.

What's going on?!?

:(

A: 

The property should be atomic I think. Just declare it as:

@property float lengthOfTime;

Updating primitives is an atomic operation.

I'm not sure if that will solve your problem or not.

The "non-atomic" keyword is for protection from multi-threading issues where one thread gets interrupted in the middle of changing it. You can only interrupt an operation if it takes more than one instruction to perform. Updating a single floating-point value is a one-instruction operation, and therefore cannot be interrupted.

Chris Cooper
Nope, didn't fix it - still zero. I've used properties before like this, I can't figure out why it's doing this.
just_another_coder
Really? Ok. Never mind then. Want to post code? I don't see why that wouldn't work.
Chris Cooper
Actually, operations on float variables (such as multiplication) can and do take more than one instruction to perform.
titaniumdecoy
A: 

It is possible that your view is reading the value before -viewDidLoad is called. I would try setting the variable in a method that is called before the view is initialized such as -awakeFromNib.

titaniumdecoy
A: 

I fixed it and don't know how I did it. That's the worst. Now I don't know how to solve it if it happens again.

I thought it wasn't being initialized, but I removed the line and it still works.

I think I'm even more frustrated than before.

just_another_coder