gpt4 book ai didi

ios - 在 Simulator Swift 中运行时出现 CLLocationCooperative2D 错误

转载 作者:行者123 更新时间:2023-11-30 12:15:11 25 4
gpt4 key购买 nike

当我在模拟器中运行我的项目时,我不断收到此错误。它只发生在 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)”的参数列表调用“index”

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

最佳答案

在此

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

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

分配类似的东西

coordinates = annotations.map { $0.coordinate }

您会意识到坐标不是 CLLocationCoodinate2D 数组,这就是为什么您不允许在此数组上调用 index(of : ) 的原因。

关于ios - 在 Simulator Swift 中运行时出现 CLLocationCooperative2D 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45509664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com