在领域数据库中保留一个CLLocationCoordinate2D数组

Dom*_*urm 3 realm swift

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

Sai*_*ira 7

正如您所说,最好的方法是创建自己的模型来存储坐标.您可以将名为class的类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)
    }
}
Run Code Online (Sandbox Code Playgroud)

持续存在的对象:

class SomeObject: Object {
    let coordinates = List<Location>()
}
Run Code Online (Sandbox Code Playgroud)

您将能够CLLocationCoordinate2D通过coordinate财产访问.例如:

someObject.coordinates[0].coordinate
Run Code Online (Sandbox Code Playgroud)