简体   繁体   English

在Simulator Swift中运行时出现CLLocationCoordinate2D错误

[英]CLLocationCoordinate2D error when running in Simulator Swift

I keep getting this error when I run my project in the simulator. 在模拟器中运行项目时,我总是收到此错误。 It only occurs in the xcode simulator. 它仅在xcode模拟器中发生。 When i run the project on my phone it seems to work perfectly. 当我在手机上运行该项目时,它似乎运行良好。

Here's my code: 这是我的代码:

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
}
}

The error seems to happen when this line is invoked in the code: 在代码中调用此行时,似乎发生了错误:

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

The error that I get reads: Cannot invoke 'index' with an argument list of type '(of:CLLocationCoodinate2D)' 我读到的错误:无法使用类型为((of:CLLocationCoodinate2D)'的参数列表调用'index'

Not sure why I get this error only in the simulator. 不知道为什么我只在模拟器中收到此错误。

In this 在这

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

What annotations contain?. 注释包含什么? I guess annotations.map { $0.coordinate } is not returning an array of CLLocationCoodinate2D for some reason. 我猜annotations.map { $0.coordinate }由于某种原因未返回CLLocationCoodinate2D数组。 Because the error says that. 因为错误说明了这一点。

assign something like 分配类似

coordinates = annotations.map { $0.coordinate }

And you will realise that coordinates is not an array of CLLocationCoodinate2D thats why you are not allowed to call index(of : ) on this array. 您将认识到坐标不是CLLocationCoodinate2D数组,这就是为什么不允许您在此数组上调用index(of:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM