Access iPhone Accelerometer Using PyObjC

I want to access the accelerometer of my iPhone through PyObjC. Here is my code:

@objc.signature("v@:@@")
def accelerometer_didAccelerate_(self, accelerometer, acceleration):
    msgBox = UIAlertView.alloc().initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles_("Acceleration", str(acceleration.x), self, "OK", None)
    msgBox.show()
    msgBox.release()

@objc.signature("v@:@")
def applicationDidFinishLaunching_(self, unused):
    self.accelerometer = UIAccelerometer.sharedAccelerometer()
    self.accelerometer.setUpdateInterval_(1)
    self.accelerometer.setDelegate_(self)
    #...

The problem is that str (accelleration.x) returns "<native-selector x of <UIAcceleration: 0x.....>>". What can I do?

+5
source share
1 answer

A- UIAcceleration xvalue is defined in Objective-C as a property and access to acceleration.xPython gives you a method named "x", not an attribute (ivar) named "x", as you might expect. You must say:

acceleration._.x

which uses the PyObjC magic accessor to get the attribute you need.

The reasons for this:

Python, . Objective-C, , , .

Objective-C 2.0, , . Objective-C :

NSLog(@"foo bar: %f", foo.bar);
foo.bar = 10.0;

(-bar -setBar:). PyObjC - Python , Objective-C, ( ), ​​ foo.bar; , , - .

+8

All Articles