【发布时间】:2018-02-23 19:57:45
【问题描述】:
我正在创建一个应用程序,其中通过在地图上长按手势来创建注释并保存在 Core Data 中。我已经查看了这个resource,但我仍然无法解决地图上出现的注释问题。 Swift 2 对我来说一切都很好,不确定发生了什么变化以及我需要做些什么来修复,因为它看起来对我来说是正确的。谢谢!这是我的代码:
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
这是我的核心数据文件:
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!")
}
}
}
还有其他核心数据文件:
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
}
【问题讨论】:
-
我在您的代码中没有看到您的获取请求 developer.apple.com/documentation/foundation/nssortdescriptor 的排序描述符。另外,您确定
saveContext()实际上正在保存并且没有因错误而失败吗?
标签: ios swift core-data swift3