ios - 从核心数据中检索 Swift 中的坐标时遇到问题
<p><p>我是编程新手,因此对我的任何无知表示歉意。我也确信我没有遵循许多最佳实践,但我的问题更具体。我正在为特定人群制作联系人应用程序。该应用程序的一部分是 mapView ,显示每个联系人的引脚。当我保存一个新人时,我正在对地址进行地理编码,如下所示:</p>
<pre><code> 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")
}
})
</code></pre>
<p>在从初始 TableView 到 mapViewController<code>viewController.fetchedPeople = fetchedResultsController.fetchedObjects</code> 到 <code>var fetchedPeople: ?</code>在 mapViewController 类中。我在 mapViewController 中有一个这样的 for 循环:</p>
<pre><code>for people infetchedPeople! {
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)
</code></pre>
<p>当我最初添加联系人时,一切都很好。引脚按预期下降。控制台显示:</p>
<pre><code>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)'
</code></pre>
<p>但是,当我关闭应用程序、重新运行并打开 mapView 时,只有一些图钉掉落。我对任何事情都进行零更改。只需关闭/打开并导航即可查看。控制台读取:</p>
<pre><code>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)
</code></pre>
<p>这只是一个例子,但似乎最好地展示了我的问题。我知道我一定是遗漏了一些东西或不了解某些东西是如何运作的,我一生都无法弄清楚自己。感谢您的宝贵时间。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>谢谢 pbasdf,这是我不明白的,完成处理程序是如何工作的。将上下文也保存在地理编码 block 中解决了我的问题。谢谢!</p></p>
<p style="font-size: 20px;">关于ios - 从核心数据中检索 Swift 中的坐标时遇到问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44729173/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44729173/
</a>
</p>
页:
[1]