使用 Core Data 在 Swift 3 中保存和检索 MKAnnotations
Saving and Retrieving MKAnnotations in Swift 3 using Core Data
在对 HitList 和 ToDo 应用程序进行详尽的搜索和构建之后,我还没有进一步实现核心数据和 MKAnnotations 的使用。我看到了关于使用 NSFetchedResults、NSUserDefaults 或其他废话的各种建议。我想知道如何实现保存 pin 位置,然后从 CoreData 中检索它。以下是我徒劳的尝试。
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var locationManager: CLLocationManager!
var storedLocations: [StoredLocations]! = []
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var markSpot: UIBarButtonItem!
@IBOutlet weak var segmentedControl: UISegmentedControl!
// Part of the Fail
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isToolbarHidden = false
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
let trackingButton = MKUserTrackingBarButtonItem(mapView: mapView)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [trackingButton, flexibleSpace, markSpot]
mapView.delegate = self
// More Fail
mapView.addAnnotation(getData() as! MKAnnotation)
}
// Miserable Fail.. Again
func getData() {
do {
storedLocations = try context.fetch(StoredLocations.fetchRequest())
}
catch {
print("Fetching Failed")
}
}
@IBAction func markSpotPressed(_ sender: Any) {
let annotation = MKPointAnnotation()
let userLatitude = mapView.userLocation.coordinate.latitude
let userLongitude = mapView.userLocation.coordinate.longitude
annotation.coordinate = CLLocationCoordinate2D(latitude: userLatitude, longitude: userLongitude)
annotation.title = "Dropped Pin"
mapView.addAnnotation(annotation)
// Another Part of the Fail
let newPin = StoredLocations(context: context)
newPin.latitude = userLatitude
newPin.longitude = userLongitude
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
你的 getData()
函数没有 return 任何东西,即使它有,你也不希望它成为 return 和 MKAnnotation
,因为你可能有一个以上的存储对象。你可以让它 return 一个 MKAnnotation
的数组
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isToolbarHidden = false
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
let trackingButton = MKUserTrackingBarButtonItem(mapView: mapView)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [trackingButton, flexibleSpace, markSpot]
mapView.delegate = self
if let annotations = getData() {
mapView.addAnnotations(annotations)
}
}
func getData() -> [MKAnnotation]? {
do {
storedLocations = try context.fetch(StoredLocations.fetchRequest())
var annotations = [MKAnnotation]()
for storedLocation in storedLocations {
let newAnnotation = MKPointAnnotation()
newAnnotation.coordinate.latitude = storedLocation.userLatitude
newAnnotation.coordinate.longitude = storedLocation.userLongitude
annotations.append(newAnnotation)
}
return annotations
}
catch {
print("Fetching Failed")
}
return nil
}
在对 HitList 和 ToDo 应用程序进行详尽的搜索和构建之后,我还没有进一步实现核心数据和 MKAnnotations 的使用。我看到了关于使用 NSFetchedResults、NSUserDefaults 或其他废话的各种建议。我想知道如何实现保存 pin 位置,然后从 CoreData 中检索它。以下是我徒劳的尝试。
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var locationManager: CLLocationManager!
var storedLocations: [StoredLocations]! = []
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var markSpot: UIBarButtonItem!
@IBOutlet weak var segmentedControl: UISegmentedControl!
// Part of the Fail
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isToolbarHidden = false
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
let trackingButton = MKUserTrackingBarButtonItem(mapView: mapView)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [trackingButton, flexibleSpace, markSpot]
mapView.delegate = self
// More Fail
mapView.addAnnotation(getData() as! MKAnnotation)
}
// Miserable Fail.. Again
func getData() {
do {
storedLocations = try context.fetch(StoredLocations.fetchRequest())
}
catch {
print("Fetching Failed")
}
}
@IBAction func markSpotPressed(_ sender: Any) {
let annotation = MKPointAnnotation()
let userLatitude = mapView.userLocation.coordinate.latitude
let userLongitude = mapView.userLocation.coordinate.longitude
annotation.coordinate = CLLocationCoordinate2D(latitude: userLatitude, longitude: userLongitude)
annotation.title = "Dropped Pin"
mapView.addAnnotation(annotation)
// Another Part of the Fail
let newPin = StoredLocations(context: context)
newPin.latitude = userLatitude
newPin.longitude = userLongitude
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
你的 getData()
函数没有 return 任何东西,即使它有,你也不希望它成为 return 和 MKAnnotation
,因为你可能有一个以上的存储对象。你可以让它 return 一个 MKAnnotation
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isToolbarHidden = false
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
let trackingButton = MKUserTrackingBarButtonItem(mapView: mapView)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbarItems = [trackingButton, flexibleSpace, markSpot]
mapView.delegate = self
if let annotations = getData() {
mapView.addAnnotations(annotations)
}
}
func getData() -> [MKAnnotation]? {
do {
storedLocations = try context.fetch(StoredLocations.fetchRequest())
var annotations = [MKAnnotation]()
for storedLocation in storedLocations {
let newAnnotation = MKPointAnnotation()
newAnnotation.coordinate.latitude = storedLocation.userLatitude
newAnnotation.coordinate.longitude = storedLocation.userLongitude
annotations.append(newAnnotation)
}
return annotations
}
catch {
print("Fetching Failed")
}
return nil
}