2017-02-26 55 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)")