【问题标题】:How to retrieve Longitude and Latitude from CoreData如何从 CoreData 中检索经度和纬度
【发布时间】:2017-09-18 07:43:27
【问题描述】:

我有这段代码,可以保存从地图视图坐标中获取的纬度和经度。当应用程序启动时,我想检索这些坐标。我确实注意到,但是当我尝试检索它时,我得到了很多我想要的其他信息。

我使用这种方法检索它,并在控制台上打印它:

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "GeoData")
    request.returnsObjectsAsFaults = false
    do
    {
        let results = try context.fetch(request)

        if results.count > 0 {
            print(results)
        }
    }
    catch
    {
        //ERROR    
    }

但是当我这样做时,我得到了这个:

 [<GeoData: 0x6000002876c0> (entity: GeoData; id: 0xd0000000014c0000 <x-coredata://1BDB903D-6F82-404A-A381-832E7932F832/GeoData/p83> ; data: {
    myLastLocation = nil;
    myLatitude = "24.16935894001965";
    myLongitude = "120.9677266312395";
})]

如何再次检索数据并获得 myLatitude 和 myLongitude 的实际值?

编辑:

我是这样保存的:

       if longPress.state == .ended 
       { 
        let touchLocation = longPress.location(in: mapView)
        let coordinate = mapView.convert(touchLocation, toCoordinateFrom: mapView)
        let newLocation = NSEntityDescription.insertNewObject(forEntityName: "GeoData", into: context)
        newLocation.setValue(coordinate.latitude  as Double, forKey: "myLatitude")
        newLocation.setValue(coordinate.longitude as Double, forKey: "myLongitude")
        do
        {
            try context.save()
        }
        catch
        {
            //ERROR
        }
       }

【问题讨论】:

  • 您是否尝试过使用 let results = try context.fetch(request) 作为 ,然后使用 results.myLatitude 和 results.myLongitude?​​span>
  • 你的意思是 let results = try context.fetch(request) as!双倍的 ?它产生了“从 [Any] 转换为不相关的类型 'Double' 总是失败”
  • 存储位置相关数据的模型类名称是什么?
  • 我不确定模型类名称。我没有设置它,我想。我会以我保存的方式添加。
  • 您还应该在问题中添加用于存储位置数据的代码。

标签: ios swift core-data core-location


【解决方案1】:

阅读您更新的问题后:

您只需要使用正确的类型声明您的 NSFetchRequest。 我从我的代码中提取了一些语句(品牌是我项目中的一个模型)。

....
    let brandRequest = NSFetchRequest<Brand>(entityName: "Brand")

    do {
                let matches = try mycontext.fetch(brandRequest)
                return matches
            }catch let error as NSError {
                ...
            }

在这种情况下,您返回的匹配项是“品牌”对象的数组。

而且,如果你的数据模型有很多属性,而你只需要经度和纬度,你可以试试NSFetchRequest.propertiesToFetch。这是我的第一个答案的意思..

【讨论】:

  • 能否详细说明如何使用?我还在努力学习这个。
【解决方案2】:

你应该喜欢

    do
    {
        let results = try context.fetch(request) as! GeoData

        if results.count > 0 {
            print("\(results.myLatitude)) & \(results.myLongitude)")
        }
    }
    catch
    {
        //ERROR    
    }

【讨论】:

  • 不幸的是,它没有用。我在 fetch 行上从 [Any] 到 unrealted 类型“GeoData 总是失败”的转换,并且“GeoData”的值类型在 if 语句中没有成员“计数”。
【解决方案3】:

我找到了解决办法:

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "GeoData")
    request.returnsObjectsAsFaults = false
    do
    {
        let results = try context.fetch(request) as! [NSManagedObject]
        if results.count > 0 {
            for result in results {
                if let latitude = result.value(forKey: "myLatitude") as? Double {
                    print(latitude)
                }
                if let longitude = result.value(forKey: "myLongitude") as? Double {
                    print(longitude)
                }
            }
        }
    }
    catch
    {
        //ERROR
    }

这个正确打印出纬度和经度,没有额外的信息。

19.7440716117355
129.099291435423

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多