我使用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;
}
);
}
答案 0 :(得分:1)
如果我理解你的问题,你想从每个模型中获取属性。
如果是这样的话,你几乎就在那里。 localWeather
就像领域世界中的数组(例如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)")