首页 > 解决方案 > 从 Realm 过滤结果

问题描述

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

[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()
}

我如何将纬度和经度分成他们自己Array的 s 或CLLocationDegrees?或者甚至是一个Array然后CLLocationDegrees用折线覆盖覆盖?

提前致谢!

标签: swiftrealm

解决方案


你可以这样做:

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

位置结构:

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)
    }

希望这会帮助你。


推荐阅读