If the event handler for Accelerometer takes a UIAcceleration *
in the following:
-(void) accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration {
then if ViewController
has an instance variable:
UIAcceleration accelerationCurrent;
Then in order to remember the acceleration as a current state, I used
accelerationCurrent = *acceleration;
in the Accelerometer event handler. But the instance variable declaration in the .h
file gave error as "Interface type cannot be statically allocated".
I thought UIAcceleration
is just 3 doubles: x, y, z, so if we can use CGPoint aPoint
there, how come we cannot use UIAcceleration accelerationCurrent
, and how can we keep the acceleration without copying the x, y, z individually but use one UIAcceleration
object? (if we declare a pointer instead, and copy the pointer (the reference), then upon leaving the accelerometer event handler, the pointer may point to an illegal memory space).
Even though UIAcceleration
has only four data members (x, y, z, and a timestamp) it is not a struct
, and therefore it does not let you refer to it statically (i.e. you need to use a pointer). This is in contrast to CGPoint
, which is a C struct
, so you can use it both through a pointer or without a pointer.
Unfortunately, there is no way around copying the values in which you are interested: you are absolutely right that once the thread leaves your handler, UIAcceleration
object may get reused behind your back. UIAcceleration
does not conform to copying protocol either, so you would need to write that copy routine yourself.