【发布时间】:2015-04-20 18:47:39
【问题描述】:
我正在尝试使用以下 python 脚本查找我的 Mac 的当前位置。它正在使用 python Objective-C 桥,它有时可以工作。但是,有时我会收到以下 AttributeError 错误,但我不确定应该如何解决该错误。
#!/usr/bin/python
# encoding: utf-8
import CoreLocation
manager = CoreLocation.CLLocationManager.alloc().init()
manager.delegate()
manager.startUpdatingLocation()
coord = manager.location().coordinate()
lat, lon = coord.latitude, coord.longitude
print lat, lon
以下错误:
Traceback (most recent call last):
File "Desktop/SimpleCoreServices.py", line 11, in <module>
coord = manager.location().coordinate()
AttributeError: 'NoneType' object has no attribute 'coordinate'
Apple's developer documentation 没有帮助我,因为我的 Objective-C 不是那么强大。
【问题讨论】:
-
那么,当
manager.location()无法获取位置时,它会返回什么?我敢打赌它会返回None。 -
你是对的
manager.location()确实返回无。当返回 None 时,我需要创建一个 try 块来让 CoreLocation 研究一个位置。谢谢你。 (对 python 来说还是新手)。 -
你可以只做
loc = manager.location()然后if loc is None(搜索位置)else coord = loc.coordinate()或类似的东西,不需要异常处理。
标签: python objective-c core-location