2017-02-26 20 views
0

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

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

回答

1

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

如果是这样,你几乎就在那里。 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)")