【问题标题】:CLLocationCoordinate2D error when running in Simulator Swift在 Simulator Swift 中运行时出现 CLLocationCoordinate2D 错误
【发布时间】:2017-08-04 14:43:33
【问题描述】:

当我在模拟器中运行我的项目时,我不断收到此错误。它只发生在 xcode 模拟器中。当我在手机上运行该项目时,它似乎运行良好。

这是我的代码:

extension QuadTreeNode: AnnotationsContainer {

@discardableResult
func add(_ annotation: MKAnnotation) -> Bool {
    guard rect.contains(annotation.coordinate) else { return false }

    switch type {
    case .leaf:
        annotations.append(annotation)
        // if the max capacity was reached, become an internal node
        if annotations.count == QuadTreeNode.maxPointCapacity {
            subdivide()
        }
    case .internal(let children):
        // pass the point to one of the children
        for child in children where child.add(annotation) {
            return true
        }

        fatalError("rect.contains evaluted to true, but none of the children added the annotation")
    }
    return true
}

@discardableResult
func remove(_ annotation: MKAnnotation) -> Bool {
    guard rect.contains(annotation.coordinate) else { return false }

    _ = annotations.map { $0.coordinate }.index(of: annotation.coordinate).map { annotations.remove(at: $0) }

    switch type {
    case .leaf: break
    case .internal(let children):
        // pass the point to one of the children
        for child in children where child.remove(annotation) {
            return true
        }

        fatalError("rect.contains evaluted to true, but none of the children removed the annotation")
    }
    return true
}

private func subdivide() {
    switch type {
    case .leaf:
        type = .internal(children: Children(parentNode: self))
    case .internal:
        preconditionFailure("Calling subdivide on an internal node")
    }
}

func annotations(in rect: MKMapRect) -> [MKAnnotation] {

    // if the node's rect and the given rect don't intersect, return an empty array,
    // because there can't be any points that lie the node's (or its children's) rect and
    // in the given rect
    guard self.rect.intersects(rect) else { return [] }

    var result = [MKAnnotation]()

    // collect the node's points that lie in the rect
    for annotation in annotations where rect.contains(annotation.coordinate) {
        result.append(annotation)
    }

    switch type {
    case .leaf: break
    case .internal(let children):
        // recursively add children's points that lie in the rect
        for childNode in children {
            result.append(contentsOf: childNode.annotations(in: rect))
        }
    }

    return result
}
}

在代码中调用此行时似乎会发生错误:

    _ = annotations.map { $0.coordinate }.index(of: annotation.coordinate).map { annotations.remove(at: $0) }

我得到的错误是:无法使用类型为“(of:CLLocationCoodinate2D)”的参数列表调用“索引”

不知道为什么我只在模拟器中收到此错误。

【问题讨论】:

  • 我真的不知道,但是模拟器可以使用CoreLocation吗?我想也许不会。
  • 是的,您可以使用 Xcode 模拟位置。奇怪的是,这对我来说似乎是编译时错误消息,而不是运行时错误的描述......
  • 它不应该在你的手机或模拟器上工作,因为CLLocationCoordinate2Ds 不是Equatable

标签: ios swift location maps


【解决方案1】:

在这

_ = annotations.map { $0.coordinate }.index(of: annotation.coordinate)

注释包含哪些内容?我猜annotations.map { $0.coordinate } 出于某种原因没有返回CLLocationCoodinate2D 的数组。 因为错误说明了这一点。

分配类似的东西

coordinates = annotations.map { $0.coordinate }

你会意识到坐标不是CLLocationCoodinate2D 的数组,这就是为什么你不能在这个数组上调用 index(of : )。

【讨论】:

  • 我相信我的代码正确无误。它在我的 iphone 上完美运行,但由于某种原因,它不能在我的笔记本电脑上的模拟器上运行。有什么原因吗?
  • 从错误信息中我只能猜到。您能否按照我的方式定义坐标并检查它得到了什么。我确信它没有得到一个 CLLocationCoodinate2D 数组。你能告诉我注释的类型吗?此处包含哪些注释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 1970-01-01
相关资源
最近更新 更多