【问题标题】:converting swift's "forced downcast(as!)" to objective c将 swift 的“强制向下转换(as!)”转换为目标 c
【发布时间】:2017-10-08 04:08:09
【问题描述】:

我想把这条 swift 行转换成 Objective-C

let location = CLLocationCoordinate2D(latitude: (marker.userData as! location).lat, longitude: (marker.userData as! location).lon)

userData 的 id 类型被强制向下转换为 location 但是当我尝试在 Objective-C 中转换 userData 时,我得到了这个:

发现多个名为“location”的方法结果不匹配, 参数类型或属性

这是我的代码:

struct CLLocationCoordinate2D markerLocation;
    markerLocation.longitude=[(CLLocation *)[marker.userData location] coordinate].longitude;

【问题讨论】:

  • 在 Objective-C 代码中,您在 userData 上调用方法“位置”。您只需要将 userData 类型转换为您的预期类型。 BTW userData 的类型是什么?
  • UserData 的类型为“id”。由于 location 是指向 CLLocation 的指针,我已将其转换为 CLLocaton 但它不起作用。我想以任何方式将那条快速线转换为Objective-C。你有什么建议吗?

标签: objective-c swift struct


【解决方案1】:

您想投射userData,而不是location 消息的结果。

markerLocation.longitude=[(CLLocation *)[marker.userData location] coordinate].longitude;

……应该是……

markerLocation.longitude=[(CLLocation *)(marker.userData) coordinate].longitude;

【讨论】:

  • 太棒了!!多谢。它完美地工作。 @Amin Negm-Awad
猜你喜欢
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多