使用 PyObjC 访问 iPhone 加速计

发布于 2024-10-21 15:20:43 字数 732 浏览 7 评论 0 原文

我想通过 PyObjC 访问 iPhone 的加速计。 这是我的代码:

@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)
    #...

问题是 str(accelleration.x) 返回 ">"。我能做些什么?

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 str(accelleration.x) returns "<native-selector x of <UIAcceleration: 0x.....>>". What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2024-10-28 15:20:43

UIAccelerationx 值在 Objective-C 中定义为 属性,并在 Python 中访问 acceleration.x 为您提供方法 名为“x”,而不是您可能期望的名为“x”的属性 (ivar)。你需要说:

acceleration._.x

它使用 PyObjC 的“神奇”访问器机制来获取你想要的属性。

原因是:

Python 类命名空间的工作方式是,不能有同名的方法和属性。这与 Objective-C 形成鲜明对比,在 Objective-C 中,getter 方法通常与其访问的属性具有相同的名称。

这种差异可能会导致与 Objective-C 2.0 的属性功能产生一些混淆,特别是因为两种语言之间的语法看起来相同。在 Objective-C 中,您可以编写:

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

它会自动使用正确的 setter 或 getter 方法(-bar-setBar:)。由于 Python 的类命名空间规则,PyObjC 无法完全执行 Objective-C 代码将执行的操作,并且必须仅选择一个事物(方法或属性)来由名称 foo.bar 表示。 >;它选择的是方法。

A UIAcceleration's x-value is defined in Objective-C as a property, and accessing acceleration.x in Python gives you the method named 'x', not the attribute (ivar) named 'x' as you might expect. You need to say:

acceleration._.x

which uses PyObjC's "magic" accessor mechanism to get the attribute you want.

The reasons for this:

The way Python's class namespaces work, you can't have a method and an attribute with the same name. This is in contrast to Objective-C where a getter method often has the same name as the attribute it accesses.

This difference can cause a little confusion with Objective-C 2.0's properties feature, especially since the syntax looks identical between the two languages. In Objective-C, you write:

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

Which automatically uses the proper setter or getter methods (-bar and -setBar:). PyObjC, because of Python's class namespace rule, can't do exactly what the Objective-C code would do, and has to pick just one thing (method or attribute) to be represented by the name foo.bar; the thing it chooses is the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文