swift - How do I update UserDefaults after deleting an MKAnnotation?
问题描述
In my project, when the user presses on the screen, a pin appears on the mapView, and the pin is saved to UserDefaults. In the bottom function, when the user selects a pin that is already on the mapView, it is deleted. However, I am unsure of how to make sure that this pin stays deleted through UserDefaults... what would I use for this last line of code?
@IBAction func addPin(_ sender: UILongPressGestureRecognizer) {
guard sender.state == .ended else { return }
let location = sender.location(in: self.mapView)
let locCoord = self.mapView.convert(location, toCoordinateFrom: self.mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = locCoord
annotation.title = titleTextField.text
self.mapView.addAnnotation(annotation)
//Create a dictionary from the annotation
let newAnnotationDict = [
"lat": locCoord.latitude,
"lng": locCoord.longitude,
"title": annotation.title
] as [String : Any]
//Pull the stored annotations data (if local)
var annotationsArray: [[String:Any]]!
var annotationsData = UserDefaults.standard.data(forKey: "StoredAnnotations")
//If the data is nil, then set the new annotation as the only element in the array
if annotationsData == nil {
annotationsArray = [newAnnotationDict]
} else {
//If it isn't nil, then convert the data into an array of dicts
do {
//Convert this data into an array of dicts
annotationsArray = try JSONSerialization.jsonObject(with: annotationsData!, options: []) as! [[String:Any]]
annotationsArray.append(newAnnotationDict)
} catch {
print(error.localizedDescription)
}
}
do {
//Use JSONSerialization to convert the annotationsArray into Data
let jsonData = try JSONSerialization.data(withJSONObject: annotationsArray, options: .prettyPrinted)
//Store this data in UserDefaults
UserDefaults.standard.set(jsonData, forKey: "StoredAnnotations")
} catch {
print(error.localizedDescription)
}
print("This will become the annotation title: \(titleTextField.text).")
print(annotation.coordinate.latitude, annotation.coordinate.longitude)
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
var selectedAnnotation = view.annotation
print("Selected Annotation: \((selectedAnnotation?.coordinate.latitude, selectedAnnotation?.coordinate.longitude))")
self.mapView.removeAnnotation(selectedAnnotation!)
// What do I use for the following line?
UserDefaults.standard.set(, forKey: "StoredAnnotations")
}
解决方案
fastest solution would be:
After deleting annotation from mapview self.mapView.removeAnnotation(selectedAnnotation!)
Convert left over mapView.annotations to an array of newAnnotationDict by doing something like this:
let newArray = self.mapView.annotations.map({ ["title": $0.title, "lat": $0.coordinate.latitude, "lng": $0.coordinate.longitude] as [String: Any]})
then serialize it into data and override (instead of appending) in UserDefaults value like you have already done in your code.
推荐阅读
- javascript - 仅当元素在屏幕上时如何播放动画
- ios - 如何使用 Capacitor 从应用程序的资产文件夹中下载图像,以便在 iOS 的照片应用程序中访问它
- email - 更新到流星 v1.11 后出错。冲突:约束 email@1.2.3 不满足 email 2.0.0
- excel - 从 Excel 中的另一个工作表的 vlookup 中为每个值重复行
- symfony - 即使在 Ctrl+Space 之后,PhpStorm 也不显示自动完成
- html5-canvas - 可以编写赛普拉斯脚本以模拟 2 个用户在同一个 HTML 画布上工作吗?
- c# - Unity 脚本在重新激活它所附加到的游戏对象后停止工作
- python-3.x - 在 python 中计算自定义概率分布(数字)
- ruby-on-rails - 使用 Apache 和 Passenger 将 Ruby 部署到生产环境
- python - 无法更新 .json 文件中的条目