Search code examples
iosarraysswiftswift3realm

Comparing the string data in the array and in the Realm database?


I need to compare the string data in the array and in the Realm database. There is this array in the code:

let months_arr = ["January","February","March","April","May","June","July","August","September","October","November","December"]

There is a created Realm database with the elements:

import Foundation
import RealmSwift
import UIKit

class EventsDB: Object {
@objc  dynamic var dataMonth = ""
@objc  dynamic var dataDay = ""
@objc  dynamic var desc = ""
@objc  dynamic var link = ""

}

(in the database is already written in the column dataMonth, the value "April")

It is necessary to compare the string values ​​"April" in months_arr and "April" in dataMonth.

I understand that to extract data from the database I will have to do something like this:

let dat = realm.objects(EventsDB)
let filter = dat.filter("dataMonth")

Solution

  • If you want to fetch an EventsDB object from your Realm whose dataMonth property is set to a certain month, you can use below piece of code:

    let month = "April"
    let events = realm.objects(EventsDB.self)
    let eventsInApril = events.filter("dataMonth == %@",month)