Crowl
Crowl

Reputation: 45

iOS - RealmSwift

I have created in my application to the database using Realmsvift. Here is the output in the console. Please tell me, how do I read data from it in the application? For example, I want to display key-values: Oslo - 2.89. Thanks for the help.

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

Upvotes: 0

Views: 36

Answers (1)

aunnnn
aunnnn

Reputation: 1952

If I understand your question correctly, you want to get properties from each model.

If that is the case, you're almost there. localWeather is like an array in the Realm's world (e.g., type of Results<WeatherData>).

You can just access it like normal swift's array/objects:

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)")

Upvotes: 1

Related Questions