我已经在 Realm 中保存了坐标数据,我试图将结果分成 2 个单独的 Array
或 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
或 CLLocationDegrees
中?或者甚至是 CLLocationDegrees
的 Array
然后用折线覆盖覆盖?
提前致谢!
最佳答案
你可以这样做:
我的项目中没有设置 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/