swift - 从 Realm 过滤结果

标签 swift realm

我已经在 Realm 中保存了坐标数据,我试图将结果分成 2 个单独的 ArrayCLLocationDegrees(纬度和经度),但是我无法弄清楚如何。我正在打印检索到的结果,所以我知道它们已成功保存/检索 - 控制台输出:

[0] Data {
    latitude = 37.33454847;
    longitude = -122.03611286;
    }, 
[1] Data {
    latitude = 37.33454218;
    longitude = -122.03638578;
    }, 
//and continues...

ViewController类(loadLocations函数)

func loadLocations() {
    theLocations = realm.objects(Data.self)
    print(theLocations)
    //This function gets called in the viewDidLoad()
}

数据类

class Data: Object {
    @objc dynamic var latitude: CLLocationDegrees()
    @objc dynamic var longitude: CLLocationDegrees()
}

我如何将纬度和经度分离到它们自己的 ArrayCLLocationDegrees 中?或者甚至是 CLLocationDegreesArray 然后用折线覆盖覆盖?

提前致谢!

最佳答案

你可以这样做:

我的项目中没有设置 Realm ,所以我正在使用结构。

位置结构:

struct Locations {

    let latitude: Double
    let longitude: Double
}

    // Your Realm result Array
    var theLocations = [Locations]()

    // Adding Dummy Data into theLocations Array
    theLocations.append(Locations(latitude: 37.33454847, longitude: -122.03611286))
    theLocations.append(Locations(latitude: 37.33454218, longitude: -122.03638578))

    // latitude and longitude to store values
    var arrLat = [Double]()
    var arrLong = [Double]()

    // Looping  through theLocations Array and Seperate latitude and longitude to append to array
    theLocations.forEach{ location in
        arrLat.append(location.latitude)
        arrLong.append(location.longitude)
    }

希望对您有所帮助。

关于swift - 从 Realm 过滤结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868167/

相关文章:

带有 BaseX 的 Java EE Realm ?

android - 使用 Realm + Okhttp 时无法获取崩溃的确切原因

json - swift 错误 : Statements are not allowed at the top level

ios - 在 iOS 上不一致地处理表情符号序列?

ios - Swift CollectionView 自定义布局单元格间距

swift - Realm swift update List 写入每个运行时

android - Realm android中的更新语句

swift - 是否可以将 UILabel 文本的常规字体更改为粗体?

ios - 使用 UIApplicationShortcutItem 启动

ios - 在 Realm 中存储来自 API 的数据