I'm creating an app where annotations are created by a long gesture tap on a map and saved in Core Data. I've checked out this resource, but I'm still having trouble with the annotations appearing on the map. Things were fine in Swift 2 for me, not sure what has changed and what I need to do to fix since it looks right to me. Thanks! Here is my code:
import UIKit
import MapKit
import CoreData
class MapViewController: UIViewController, MKMapViewDelegate {
//Variables
var locationManager = CLLocationManager()
var currentPins = [Pin]()
var gestureBegin: Bool = false
var sharedContext: NSManagedObjectContext {
return CoreDataStack.sharedInstance().managedObjectContext
}
//Fetch All Pins
func fetchAllPins() -> [Pin] {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Pin")
do {
return try sharedContext.fetch(fetchRequest) as! [Pin]
} catch {
print("Error In Fetch!")
return [Pin]()
}
}
//Outlets
@IBOutlet weak var mapView: MKMapView!
//Core Dat
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(MapViewController.longPressGesture(longPress:)))
longPressRecognizer.minimumPressDuration = 1.5
mapView.addGestureRecognizer(longPressRecognizer)
self.mapView.delegate = self
addSavedPinsToMap()
}
//Add Saved Pin To Map
func addSavedPinsToMap() {
currentPins = fetchAllPins()
print("Pins Count in Core Data Is \(currentPins.count)")
for currentPin in currentPins {
let annotation = MKPointAnnotation()
annotation.coordinate = currentPin.coordinate
mapView.addAnnotation(annotation)
}
}
//Long Press Gesture Recognizer
func longPressGesture(longPress: UIGestureRecognizer) {
//Take Point
let touchPoint = longPress.location(in: self.mapView)
//Convert Point To Coordinate From View
let touchMapCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)
//Init Annotation
let annotation = MKPointAnnotation()
annotation.coordinate = touchMapCoordinate
//Init New Pin
let newPin = Pin(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude, context: sharedContext)
//Save To Core Data
CoreDataStack.sharedInstance().saveContext()
//Adding New Pin To Pins Array
currentPins.append(newPin)
//Add New Pin To Map
mapView.addAnnotation(annotation)
}
} // End Class
Here is my Core Data file:
import Foundation
import CoreData
import MapKit
//Pin Class
public class Pin: NSManagedObject {
var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: latitude, longitude: longitutde)
}
convenience init(latitude: Double, longitude: Double, context: NSManagedObjectContext) {
if let ent = NSEntityDescription.entity(forEntityName: "Pin", in: context) {
self.init(entity: ent, insertInto: context)
self.latitude = latitude
self.longitutde = longitude
} else {
fatalError("Unable To Find Entity Name!")
}
}
}
And the other Core Data file:
import Foundation
import CoreData
extension Pin {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Pin> {
return NSFetchRequest<Pin>(entityName: "Pin")
}
@NSManaged public var latitude: Double
@NSManaged public var longitutde: Double
}