gpt4 book ai didi

iOS-RealmSwift

转载 作者:行者123 更新时间:2023-11-28 15:49:48 26 4
gpt4 key购买 nike

我已经使用 Realmsvift 在我的应用程序中创建了数据库。这是控制台中的输出。请告诉我,如何在应用程序中从中读取数据?例如,我想显示键值对:Oslo - 2.89。感谢您的帮助。

class ViewController: UIViewController {
var city_list: [String] = ["Moscow", "London", "Oslo", "Paris"]
let realm = try! Realm()
override func viewDidLoad() {
super.viewDidLoad()
let manager: ManagerData = ManagerData()
for name in city_list {
manager.loadJSON(city: name)
}
let localWeather = realm.objects(WeatherData.self)
print(localWeather)

/////////////////////////////////////

Results<WeatherData> (
[0] WeatherData {
city_name = Moscow;
tempList = RLMArray <0x6080002e2b00> (
[0] Temp {
temp = -4.25;
}
);
},
[1] WeatherData {
city_name = London;
tempList = RLMArray <0x6000002e4700> (
[0] Temp {
temp = 9.630000000000001;
}
);
},
[2] WeatherData {
city_name = Paris;
tempList = RLMArray <0x6000002e4800> (
[0] Temp {
temp = 6.59;
}
);
},
[3] WeatherData {
city_name = Oslo;
tempList = RLMArray <0x6000002e4900> (
[0] Temp {
temp = -2.89;
}
);
}

最佳答案

如果我正确理解你的问题,你想从每个模型中获取属性。

如果是这样的话,你就快成功了。 localWeather就像 Realm 世界中的数组(例如 Results<WeatherData> 的类型)。

您可以像普通 swift 的数组/对象一样访问它:

let firstWeather = localWeather[0]
let name = firstWeather.city_name
let temp = firstWeather.tempList[0].temp

// do what you want with 'name' and 'temp', e.g. key-value
print("\(name) - \(temp)")

关于iOS-RealmSwift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com