【问题标题】:Contextual Type AnyObject Cannot Be Used With Array Literal for Coordinates上下文类型 AnyObject 不能与数组文字一起用于坐标
【发布时间】:2016-09-19 18:22:08
【问题描述】:

我已经查看了发布相同问题的其他几个不同问题,但没有一个有助于回答我的问题,因为我正在使用它作为坐标。我最近升级到 Swift3 并收到此错误“上下文类型 AnyObject 不能与 Array Literal 一起使用

这就是我为坐标声明变量的代码的样子

 var coordinates: [AnyObject]!

这是出现错误的下一个代码,我将用x替换数字。我用的是经度和纬度

coordinates = [[xx.xxxxxx, -xxx.xxxxxx],[xx.xxxxxx, -xxx.xxxxxx],[xx.xxxxxx, -xxx.xxxxxx]]

我称它的方式是使用 for 循环,我认为这不会导致任何问题,但我会发布它以防万一

for i in 0...2
    {
        let coordinate = coordinates[i]}

通过实施解决问题

var coordinates: [[Double]]!

【问题讨论】:

  • 有什么原因不能将坐标声明为var coordinates: [[Float]]!var coordinates: [[Double]]!
  • 为什么使用[AnyObject]! 类型的二维数组?编译器不知道内部数组应该是什么类型。在任何情况下,更合适的数据结构应该是结构数组。
  • 实际的链接器错误是什么?
  • 链接器命令失败,退出代码为 1(使用 -v 查看调用)ld:找不到框架 Pods_getCurrentLocation clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)跨度>

标签: ios iphone arrays swift xcode


【解决方案1】:

只需改变:-

import MapKit


class yourController : ...{  

var coordinates: [AnyObject]!

var coordinates = [CLLocationCoordinate2D]()

要访问特定索引处的特定latlong:-

coordinates[your_index].latitude // Latitude

coordinates[your_index].longitude //Longitude

CLLocationCoordinate2D 是一个结构体,比数组更容易扩展和访问。

【讨论】:

  • 避免在回答一个问题时延长您的问题,因此一天内发布两个问题没有限制。如果它解决了您的问题,请接受它作为答案。:)快乐编码
  • 还有更多问题,我在实现不同部分时遇到了更多问题。请阅读帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多