我在模擬器中運行我的項目時不斷收到此錯誤。它只發生在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)'的參數列表'
不知道爲什麼我只在模擬器中出現此錯誤。
我真的不知道,但模擬器可以使用CoreLocation嗎?我想也許不會。 – dfd
是的,你可以使用Xcode模擬位置。奇怪的是,這似乎是一個編譯時錯誤消息給我,而不是一個運行時錯誤的描述... –
它不應該在你的手機或模擬器上工作,因爲'CLLocationCoordinate2D''不是'Equatable' – dan