swift - 从 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
用折线覆盖覆盖?
提前致谢!
解决方案
你可以这样做:
我的项目中没有设置领域,所以我正在使用结构。
位置结构:
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)
}
希望这会帮助你。
推荐阅读
- google-chrome - 使用 Tomcat 时,是否可以自动重新加载客户端文件(js、css、html)而无需在 Google Chrome 中手动刷新页面?
- python - 如何在 Python 中指定函数的输入类型?
- python - 将嵌套字典转换为 json
- flutter - 未处理的异常:“String”类型不是“Uri”类型的子类型
- python - 来自 url 的 Pandas read_csv 返回更少的行数
- .htaccess - 如何通过将显示语言添加为子文件夹在 htaccess 中创建多语言 url
- php - 在 laravel 中找不到类文件?
- flutter - 订阅状态没有从一个屏幕传递到另一个“inAppPurchase”
- android - Android:从片段更新活动中底部导航视图的徽章计数
- android - 如何在我的 recyclerview 中使用 cardview 设置 onclick 侦听器,并在我单击 recyclerview 时从 firebase 数据库中检索数据?