gpt4 book ai didi

swift - 在 Realm 数据库中保存一个 CLLocationCoordinate2D 数组

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:13 24 4
gpt4 key购买 nike

我正在编写一个基于位置的应用程序,我需要在我的 Realm 模型中保存一个 CLLocationCoordinate2D 数组。最好的方法是什么?我应该为坐标定义一个新的模型对象并使用 List 来保存该数组还是有更好的方法?

最佳答案

正如您所说,最好的方法是创建您自己的模型来存储坐标。您可以将类命名为 Location这将是 CLLocationCoordinate2D 的模型和其他 Realm 对象可以使用 List<Location> 持久化它或者只是 Location .

定义Location类:

class Location: Object {
dynamic var latitude = 0.0
dynamic var longitude = 0.0

/// Computed properties are ignored in Realm
var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2D(
latitude: latitude,
longitude: longitude)
}
}

以及持久化它们的对象:

class SomeObject: Object {
let coordinates = List<Location>()
}

您将能够访问 CLLocationCoordinate2D通过coordinate属性(property)。例如:

someObject.coordinates[0].coordinate

关于swift - 在 Realm 数据库中保存一个 CLLocationCoordinate2D 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38038650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com