2017-08-04 55 views
0

我在模拟器中运行我的项目时不断收到此错误。它只发生在xcode模拟器中。当我在手机上运行该项目时,它似乎完美地工作。在模拟器Swift中运行时发生CLLocationCoordinate2D错误

这里是我的代码:

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

,我得到的错误读取:无法调用“索引”与“(CLLocationCoodinate2D)'的参数列表'

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

+0

我真的不知道,但模拟器可以使用CoreLocation吗?我想也许不会。 – dfd

+0

是的,你可以使用Xcode模拟位置。奇怪的是,这似乎是一个编译时错误消息给我,而不是一个运行时错误的描述... –

+0

它不应该在你的手机或模拟器上工作,因为'CLLocationCoordinate2D''不是'Equatable' – dan

回答

0

在这种

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

什么注释包含?由于某种原因,我想annotations.map { $0.coordinate }没有返回数组CLLocationCoodinate2D。 因为错误说明了这一点。

分配的东西像

coordinates = annotations.map { $0.coordinate } 

而且你会发现,坐标是不是CLLocationCoodinate2D这就是为什么你不能打电话(作者:)指数数组中此阵列上。

+0

我相信我的代码是正确无误的。它在我的iphone上完美运行,但由于某种原因,它不能在我的笔记本电脑上运行在我的模拟器上。任何可能的原因? – ajayb

+0

从错误信息我只能猜到。你能否按照我所做的方式定义坐标,并检查它得到的结果。我相信它没有获得CLLocationCoodinate2D的数组。您能否让我知道注释的类型?这里包含哪些注释。 –

相关问题