objective-c - 带有纬度和经度的 swift 中的 CLLocation 对象

标签 objective-c swift cllocation

我会尝试设置 CLLocation 对象的纬度和经度属性,但在运行时出现此错误:

Cannot convert CLlocation object to EMTrackPoint Object

enter image description here

for element in objectsArray {
    let item = CLLocation(latitude: element.latitude, longitude: element.longitude) as! EMTrackPoint

    item.mElevation = element.ele
    item.mOrientation = element.orientation
    item.mDate = Date()
    if element.poiActive != ""{
         GPXActive.add(element.poiActive.components(separatedBy: ","))
    }
    GPXDanger.add(element.dangerActive.components(separatedBy: ","))
    item.mActivePOIs = GPXActive
    item.mActiveDangers = GPXDanger
    GPXPoints.add(item)
}

Objective-C 中的 EMTrackPoint 类:

@interface EMTrackPoint : CLLocation

@property (nonatomic, assign) double mElevation;
@property (nonatomic, retain) NSDate *mDate;
@property (nonatomic, retain) NSMutableArray *mActivePOIs;
@property (nonatomic, retain) NSMutableArray *mActiveDangers;
@property (nonatomic, assign) double mOrientation;
@property (nonatomic, assign) double mSpeed;

请帮忙。

最佳答案

每个 EMTrackPoint 也是一个 CLLocation 那么为什么不首先初始化一个 EMTrackPoint 实例而不是进行类型转换:

for element in objectsArray {
    let item = EMTrackPoint(latitude: element.latitude, longitude: element.longitude)
    item.mElevation = element.ele
    item.mOrientation = element.orientation
    item.mDate = Date()
    ...

关于objective-c - 带有纬度和经度的 swift 中的 CLLocation 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47247427/

相关文章:

iphone - NSMutableArray removeObjectAtIndex 导致错误

objective-c - 通用 iOS 应用程序中的 iPad/iPhone 应用程序委托(delegate)有什么意义?

swift - UIAlertController 中的倒计时

swift - 将不带分隔符的字符串转换为 Double/CLLocationDegrees

iphone - 不会触发任何与方向相关的通知。为什么?

ios - Swift 和 Objective-c 框架公开其内部结构

ios - 如何将数据写入项目目录中的文件?

ios - 数组扩展以按值删除对象

ios - 为什么我的注释中包含 MKUserLocation?

ios - 获取 CLLocation 类(class)并翻译给用户