我是编程新手,所以对我的任何无知道歉。我也确信我没有遵循许多最佳实践,但我的问题更具体。我正在为特定人群制作联系人应用。该应用程序的一部分是一个地图视图,显示每个联系人的引脚。当我保存一个新人时,我正在对地址进行地理编码:
let entity = NSEntityDescription.entity(forEntityName: "Person", in: self.managedObjectContext)
let record = NSManagedObject(entity: entity!, insertInto: self.managedObjectContext)
geocoder.geocodeAddressString(rvAddress, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error!)
}
if let placemark = placemarks?.first {
let rvCoordinates = placemark.location!.coordinate
let rvLatitude = rvCoordinates.latitude
let rvLongitude = rvCoordinates.longitude
record.setValue(rvLatitude, forKey: "latitude")
record.setValue(rvLongitude, forKey: "longitude")
}
})
在从初始表视图到地图视图控制器viewController.fetchedPeople = fetchedResultsController.fetchedObjects
到var fetchedPeople: [Person]?
的segue中填充已提取的人员在地图视图控制器类中。我在地图视图控制器中有这样的for循环:
for people in fetchedPeople! {
let firstName = people.value(forKey: "firstName") as? String
let lastName = people.value(forKey: "lastName") as? String
let street = people.value(forKey: "street") as? String
let rvLatitude = people.value(forKey: "latitude") as? Double
let rvLongitude = people.value(forKey: "longitude") as? Double
print("LAT: \(rvLatitude!) LONG: \(rvLongitude!)")
let rvCoordinates = CLLocationCoordinate2D(latitude: rvLatitude!, longitude: rvLongitude!)
var personName: String!
var personAddress: String!
if firstName != nil {
personName = firstName! + " " + lastName!
} else {
personName = lastName!
}
print(personName)
personAddress = street!
let mapPerson = MapPerson(title: personName, locationName: personAddress, coordinate: rvCoordinates)
//mapPeopleArray.append(mapPerson)
mapView.addAnnotation(mapPerson)
print(rvCoordinates)
当我最初添加联系人时,一切都很棒。针脚按预期下降。控制台显示:
LAT: 41.1656913 LONG: -81.8692497
Eric Madzay
CLLocationCoordinate2D(latitude: 41.165691299999999, longitude: -81.869249699999997)
LAT: 41.253647 LONG: -81.875974
Ethan Madzay
CLLocationCoordinate2D(latitude: 41.253647000000001, longitude: -81.875973999999999)
LAT: 41.203748 LONG: -81.858518
Work
CLLocationCoordinate2D(latitude: 41.203747999999997, longitude: -81.858518000000004)'
然而,当我关闭应用程序,重新运行并打开地图视图时,只有部分引脚掉线。我对任何东西都做零改变。仅关闭/打开并导航到查看。控制台读取:
LAT: 41.253647 LONG: -81.875974
Ethan Madzay
CLLocationCoordinate2D(latitude: 41.253647000000001, longitude: -81.875973999999999)
LAT: 41.1656913 LONG: -81.8692497
Eric Madzay
CLLocationCoordinate2D(latitude: 41.165691299999999, longitude: -81.869249699999997)
LAT: 0.0 LONG: 0.0
Work
CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
这只是一个例子,但似乎最好地展示了我的问题。我知道我必须遗漏某些东西或者不理解某些东西是如何发挥作用的,我只是无法弄清楚自己的生活。谢谢你的时间。
答案 0 :(得分:0)
谢谢pbasdf,这是我不理解的,完成处理程序是如何工作的。在地理编码块中保存上下文也解决了我的问题。谢谢!