2017-08-04 52 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的數組。您能否讓我知道註釋的類型?這裏包含哪些註釋。 –

相關問題